js time conversion

//Convert time to timestamp
function DateToUnix(string) {
var f = string.split(' ', 2);
var d = (f[0] ? f[0] : '').split('-', 3);
var t = (f[1] ? f[1] : '').split(':', 3);
var time = (new Date(
parseInt(d[0], 10) || null,
(parseInt(d[1], 10) || 1) - 1,
parseInt(d[2], 10) || null,
parseInt(t[0], 10) || null,
parseInt(t[1], 10) || null,
parseInt(t[2], 10) || null
)).getTime() / 1000
return time;
}
// Convert GMT time format to string
function GMTToStr(time) {
let date = new Date (time)
let Str = date.getFullYear() + '-' +
(date.getMonth() + 1) + '-' +
date.getDate() + ' ' +
date.getHours() + ':' +
date.getMinutes() + ':' +
date.getSeconds()
return Str
}

//The time is converted to GMT format
function parserDate(date) {
var t = Date.parse(date);

if(!isNaN(t)) {
return new Date(Date.parse(date.replace(/-/g, "/")));
} else {
return new Date();
}
};

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325640817&siteId=291194637
Recommended