日期格式化

/**
	 *  把日期转化为英文表示方式
	 * @param datatime 格式为 1323337201000毫秒数,或者日期字符串格式为2011-12-08 17:40:01
	 * @return 格式为"Thursday,December 08,2011 Posted :17:40 BJT(0940 GMT)" 
	 */
	function parseToEnglishData(datetime){
		//少于10前面加0
		var toLong = function(h){
			if(h<10){
				return "0"+h;
			}else{
				return h;
			}
		};

		//字符串格式为2011-12-08 17:40:01
		var dateParse = function(dateTimeStr) {
			var mr = /^(\d{4})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/.exec(dateTimeStr);
			if (mr) {
				var d = new Date(parseInt(mr[1], 10),
				parseInt(mr[2], 10) - 1,
				parseInt(mr[3], 10),
				parseInt(mr[4], 10),
				parseInt(mr[5], 10),
				parseInt(mr[6], 10));
				return d;
			} else {
				alert("格式不正确!");
			}
		};

	

		var dayArray = ["Mondey","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
		var monthArray = ["January","February","Marcy","April","May","June","July","August","September","October","November","December"];
		var total ="";
		var totalTime ;
		if(typeof datetime == "number"){
			totalTime = datetime;
		}else if(typeof datetime == 'string'){
			totalTime=dateParse(datetime).getTime();
		}
		var d = new Date(totalTime);
		var utcHours = d.getUTCHours(); //时
		var utcMinute = d.getUTCMinutes(); //分
		//==BJ
		var bjYear = d.getFullYear();//年
		var bjMonth = d.getMonth(); //月份
		var bjDate = d.getDate(); //日
		var bjDay = d.getDay(); //星期
		var bjHours = d.getHours(); //时
		var bjMinute = d.getMinutes(); //分
		
		total+=dayArray[bjDay]+","
		total+=monthArray[bjMonth]+" ,";
		total+=bjYear+" Posted :";
		total+=toLong(bjHours)+":";
		total+=toLong(bjMinute)+" BJT(";
		total+=toLong(utcHours)+toLong(utcMinute)+" GMT)";
		//alert(total);
		return total;
	}
		//进行转换
		var s = parseToEnglishData(1323337201000);
		alert(s);

		//字符串
		var s1 = parseToEnglishData("2011-12-08 17:40:01");
		alert(s1)
 

你可能感兴趣的:(日期格式化)