时间戳和时间格式的相互转化

window.onload = function() {
	//将时间戳(十三位时间搓,也就是带毫秒的时间搓)转换成时间格式
	let cTime = 1539083829000
	let date = new Date(cTime);
	let year = date.getFullYear();
	let month = date.getMonth()+1;
	let day = date.getDate();
	let h = date.getHours();
	let m = date.getMinutes();
	let s = date.getSeconds();
	month = month < 10 ? "0"+month:month;
	day = day < 10 ? "0"+day:day;
	h = h < 10 ? "0"+h:h;
	m = m < 10 ? "0"+m:m;
	s = s < 10 ? "0"+s:s;
	date = year+'-'+month+'-'+day+' '+h+':'+m+':'+s;
	console.log(date);  //2018-10-09 19:17:09

	
	// 时间格式转化为时间戳
	var nowTime = '2018-10-09 19:17:09';
	var time=new Date(nowTime).valueOf()
	console.log(time); 	//1539083829000
	
}

猜你喜欢

转载自blog.csdn.net/xiaoma19941027/article/details/107363147