JS中时间戳转换日期格式函数

function timestampToTime(now) {
		//其中now就是一个Date类型
		//如果需要字符串型的时间戳,则需要创建一个Date类型
		// var date = new Date(timestamp);   //其中timestamp为字符串 
		var year=now.getFullYear(); 
		var month=now.getMonth()+1 < 10 ?  '0'+(now.getMonth()+1) : (now.getMonth()+1); 
		var date=now.getDate() < 10 ?  '0'+(now.getDate()) : (now.getDate());
		var hour=now.getHours() < 10 ?  '0'+(now.getHours()) : (now.getHours());
		var minute=now.getMinutes() < 10 ?  '0'+(now.getMinutes()) : (now.getMinutes()); 
		var second=now.getSeconds() < 10 ?  '0'+(now.getSeconds()) : (now.getSeconds()); 
		return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second; 
		}

猜你喜欢

转载自blog.csdn.net/loster_li/article/details/79965927