js将GMT格式转换成数据库使用格式

const DateParse = {

/**
* 将GMT格式的时间转为数据库兼容的Date格式
* gmt => GMT格式的时间
* long => 是否长时间(包含时分秒), 可不填(默认值:true), 选填:false
* Wed Dec 01 2018 00:00:00 GMT+0800 (中国标准时间) => 2018-12-01 00:00:00 或 2018-12-01
*/
formatGMT: ( gmt, long ) => {

long = false === long ? false : true

let date = new Date(gmt);

let m = (date.getMonth() + 1), mm = '-' + (m < 10 ? '0' + m : m);
let d = date.getDate(), dd = '-' + (d < 10 ? '0' + d : d);
let h = date.getHours(), hh = ' ' +(h < 10 ? '0' + h : h);
let i = date.getMinutes(), ii = ':' +(i < 10 ? '0' + i : i);
let s = date.getSeconds(), ss = ':' +(s < 10 ? '0' + s : s);

return date.getFullYear() + mm + dd + (long?(hh + ii + ss):'')
}
}

猜你喜欢

转载自www.cnblogs.com/hmpcly/p/10144566.html