js timestamp format, jsonDate formatted

json date format

{date: 15, hours: 10, seconds: 28, month: 0, nanos: 0, …}>>>>2020-01-15 10:01

//json日期格式转换为正常格式
var jsonDate = {
		date: 15,
		hours: 10,
		seconds: 28,
		month: 0,
		nanos: 0,
		timezoneOffset: -480,
		year: 120,
		minutes: 1,
		time: 1579053688000,
		day: 3
	}
function jsonDateFormat(jsonDate) {

    try {
	 var year = jsonDate.year+1900,
		 month = jsonDate.month+1,
		 day = jsonDate.date,
		 hour=jsonDate.hours,
		 minute=jsonDate.minutes,
		 hour=(hour>9)?(""+hour):("0"+hour),
		 minute=(minute>9)?(""+minute):("0"+minute),
		 month=(month>9)?(""+month):("0"+month);  //如果得到的数字小于9要在前面加'0'
		 day=(day>9)?(""+day):("0"+day);
    return year + "-" + month + "-" + day +" "+hour+":"+minute;

    } catch (ex) {
        return "";
    }
    
}

Timestamp conversion
1579152254000 >>>>>>> 2020-01-1509 :: 54: 02

//时间戳转换为正常格式
 function timestampTostring (timestamp){

        if (timestamp == null || timestamp == "") {
            return "";
        }
         var date = new Date(timestamp);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
         Y = date.getFullYear() + '-';
         M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
         D = date.getDate();
         H = (date.getHours()<10?'0'+date.getHours():date.getHours()) +':';
         m = ':'+(date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes());
         s = ':'+(date.getSeconds()<10?'0'+date.getSeconds():date.getSeconds());
         return Y+M+D+' '+H+m+s;

}
Published 51 original articles · won praise 18 · views 20000 +

Guess you like

Origin blog.csdn.net/m0_37882063/article/details/104003486