HTML5网页设计

时间日期

Javascript时间戳转日期时间方法

时间:2017-01-04 19:15来源:未知作者:user点击:
时间戳转日期格式的2中办法:
时间戳转日期格式的2中办法:

方法一:
function formatDate(timestamp){
	var time = new Date(timestamp);
	var year = time.getFullYear();
	var month = time.getMonth()+1;
	var date = time.getDate();
	var hour = time.getHours();
	var minute = time.getMinutes();
	var second = time.getSeconds();
	month=month<10?"0"+month:month; 
	date=date<10?"0"+date:date;
	hour=hour<10?"0"+hour:hour;
	minute=minute<10?"0"+minute:minute;
	second=second<10?"0"+second:second;
	time = year + '-' + month + '-' + date + ' ' + hour + ':' + minute + ':' + second ;
	return time;
}

方法二:
function formatDate(timestamp){
	var time = new Date(timestamp);
	var year = time.getFullYear();
	var month = "0" + time.getMonth()+1;
	var date = "0" + time.getDate();
	var hour = "0" + time.getHours();
	var minute = "0" + time.getMinutes();
	var second = "0" + time.getSeconds();
	time = year + '-' + month.substr(-2) + '-' + date.substr(-2) + ' ' + hour.substr(-2) + ':' + minute.substr(-2) + ':' + second.substr(-2) ;
	return time;
}

上一篇:上一篇:显示当前时间JS代码 上一篇:下一篇:没有了
阅完本篇文章?您还可以: 收藏本文 文章挑错 朋友推荐