时间转换:将秒转换为年月日时分秒以及获取当前时间的方法

1、将秒转化为年月日时分秒的格式

// 将秒转化为年月日时分秒
function secToTime(seconds){
	var date = new Date(seconds*1000)
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var day = date.getDate();
	var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
	var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
	var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
	if(month<10) month ="0"+month;
	if(day<10) day ="0"+day;
	var currentTime = year + "-" + month + "-" + day + "  " + hour + ":" + minute + ":" + second;
	return currentTime;
}
secToTime(1584090728);

结果如下图所示:
时间转换:将秒转换为年月日时分秒以及获取当前时间的方法_第1张图片
2、获取当前的时间

// 获取当前的时间(年月日)
function dateToday(){
	var d = new Date();
	var y = d.getFullYear(); // 年
	var m = d.getMonth() + 1; // 月份从0开始的
	var d = d.getDate(); //日
	if(m<10)m = "0" + m;
	if(d<10)d = "0" + d;
	var today = y + '-' + m + '-' + d;
	return today;
}
dateToday()

结果如下图所示:
时间转换:将秒转换为年月日时分秒以及获取当前时间的方法_第2张图片

你可能感兴趣的:(js)