时间格式转换

 

标准时间转换函数

string (”2012-03-08 15:28:30“) => javascript:Date

javascript Date => string

function parseDate (str) {
		s1 = str.split(' ');
		s2 = s1[0].split('-');
		s3 = s1[1].split(':');
		return new Date(s2[0],s2[1]-1,s2[2],s3[0],s3[1],s3[2]);
	}

	function formatDate (date) {
		y = date.getFullYear();
		m = padLeft((date.getMonth()+1),2);
		d = padLeft(date.getDate(),2);
		h = padLeft(date.getHours(),2);
		min = padLeft(date.getMinutes(),2);
		s = padLeft(date.getSeconds(),2);
		return [y,m,d].join('-')+' '+[h,min,s].join(':');
	}
	
	function padLeft(str, lenght) {
		str = new String(str);
		if (str.length >= lenght)
			return str;
		else
			return padLeft("0" + str, lenght);
	}

	str_d = '2013-1-8 3:1:5';
	d = parseDate(str_d);
	console.debug(str_d + '==?' + d.toLocaleString());
	console.debug(str_d+"==?"+formatDate(d));

 

 console:

2013-1-8 3:1:5==?Tue Jan 08 2013 03:01:05 GMT+0800 (中国标准时间) timeformat_parse.html:41

2013-1-8 3:1:5==?2013-01-08 03:01:05

 

你可能感兴趣的:(时间格式转换)