Js time format and timestamp conversion sample code
- 2020-03-30 01:04:15
- OfStack
Time conversion timestamp
Two. Timestamp conversion time
(1) : convert to the format of 2011-3-16 16:50:43:
(2) : converted to 16:50:43, March 16, 2011:
(3) : converted to 16:50 on March 16, 2011
function transdate(endTime){
var date=new Date();
date.setFullYear(endTime.substring(0,4));
date.setMonth(endTime.substring(5,7)-1);
date.setDate(endTime.substring(8,10));
date.setHours(endTime.substring(11,13));
date.setMinutes(endTime.substring(14,16));
date.setSeconds(endTime.substring(17,19));
return Date.parse(date)/1000;
}
Two. Timestamp conversion time
(1) : convert to the format of 2011-3-16 16:50:43:
function getDate(tm){
var tt=new Date(parseInt(tm) * 1000).toLocaleString().replace(/ years | month /g, "-").replace(/ day /g, " ")
return tt;
}
(2) : converted to 16:50:43, March 16, 2011:
function getDate(tm){
var tt=new Date(parseInt(tm) * 1000).toLocaleString()
return tt;
}
(3) : converted to 16:50 on March 16, 2011
function getDate(tm){
var tt=new Date(parseInt(tm) * 1000).toLocaleString().substr(0,16);
return tt;
}