数据处理:毫秒数或标准时间转“年月日 时分秒”的格式

毫秒数转日期格式

代码:

//日期格式函数
function dateFormat(str){
    var oDate = new Date(str),
    oYear = oDate.getFullYear(),
    oMonth = oDate.getMonth()+1,
    oDay = oDate.getDate(),
    oHour = oDate.getHours(),
    oMin = oDate.getMinutes(),
    oSen = oDate.getSeconds(),
    return oYear+ "年" + getzf(oMonth, 2) + "月" + getzf(oDay, 2)+"日 "+getzf(oHour, 2)+":"+getzf(oMin, 2)+ ":"+ getzf1(oSen, 2);
};

//补0操作1
function getzf(num){
    if(parseInt(num) < 10){
        num = '0'+num;
    }
    return num;
}  

就可将13位 的“1508303298972” 毫秒数 转成 “2017年10月18日 13:08:18”

时间标准时间转为固定的时间格式

    //添0占位
function formatTen(num) { 
	return num > 9 ? (num + "") : ("0" + num); 
} 
function formatDate(date) { 
	var date = new Date(date)
	var year = date.getFullYear(); 
	var month = date.getMonth() + 1; 
	var day = date.getDate(); 
	var hour = date.getHours(); 
	var minute = date.getMinutes(); 
	var second = date.getSeconds(); 
	return year + "-" + formatTen(month) + "-" + formatTen(day)+ " " +formatTen(hour)+ ":" +formatTen(minute)+ ":" +formatTen(second); 
} 

就可将标准时间“2019-04-17T10:30:40.489+08:00” 转成 “2019-04-17 10:30:40”

原文地址:https://www.cnblogs.com/missme-lina/p/10246802.html