js里Date时间格式的常用转换-------(GMT转成yyyy-MM-dd)--------(根据日期获得星期几)

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):'')
}
}


/**
 * 根据日期字符串获取星期几
 * @param dateString 日期字符串(如:2016-12-29),为空时为用户电脑当前日期
 * @returns {String}
 */
function getWeek(dateString){
    var date;
    if(isNull(dateString)){
        date = new Date();
    }else{
        var dateArray = dateString.split("-");
        date = new Date(dateArray[0], parseInt(dateArray[1] - 1), dateArray[2]);
    }
    //var weeks = new Array("日", "一", "二", "三", "四", "五", "六");
    //return "星期" + weeks[date.getDay()];
    return "星期" + "日一二三四五六".charAt(date.getDay());
};


posted @ 2018-12-21 09:25  闭眼的鱼  阅读(2625)  评论(0编辑  收藏  举报