1. 程式人生 > >給javascript的Date新增parse和format函式

給javascript的Date新增parse和format函式

前端開發中遇到日期字串的解析和格式化問題,為了滿足更多情況,於是花心思自己寫了一個版本,適用較多情況,如下:

var date = new Date();
date = date.parse("Tues,二月 07 2017 21:36:05.234", "[W:week],[M:月] DD YYYY hh:mm:ss.xxx");
var str = date.format("YYYY-MM-DD");
console.log(str); // 2017-02-07
/*  amuliang 2017/2/7 22:41 [email protected]
    parse函式基於types解析,對應關係如下
        Y 年
        M 月
        D 日
        W 星期
        h 時
        m 分
        s 秒
        x 毫秒
    每種型別可以有多個長度,且它們的分割方式任意,如YYYY-MM-DD, YY,MM,DD
    並且支援詞語解析,格式為[type:label],如[M:month],且必須有明顯的分隔字元,
    如"[M:month] YYYY",不能寫成"[M:month]YYYY",因為詞語長度不固定如Sunday與Tuesday長度就不相同

    下面是用法示例:
    var date = new Date();
    date.parse("星期二,二月 07 2017 21:36:05.234", "[W:星期],[M:月] DD YYYY hh:mm:ss.xxx");
*/
;(function() {
//*****************************************************
var datestr, dateformat, index, char;
var types = {
    "Y": {
        result: 0,
    },
    "M": {
        words: {
            "月": ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
            "month": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
            "fullmonth": ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
        },
        result: 0,
    },
    "D": {
        result: 0,
    },
    "W": {
        words: {
            "星期": ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
            "week": ["Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat"],
            "fullweek": ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
        },
        result: 0,
    },
    "h": {
        result: 0,
    },
    "m": {
        result: 0,
    },
    "s": {
        result: 0,
    },
    "x": {
        result: 0,
    },
}
//
function init(str, format) {
    datestr = str;
    dateformat = format;
    index = 0;
    char = null;
    for(var i in types) {
        types[i].result = 0;
    }
}

function getWordByNum(type, label, num) {
    //
    return types[type][label][num];
}
function getNumByWord(type, label, word) {
    var words = types[type].words[label];
    for(var i = 0; i < words.length; i++) {
        if(word.match(words[i])) return i;
    }
    return 0;
}

function charNext(num) {
    if(num) index += num - 1;
    char = index < dateformat.length ? dateformat[index++] : null;
    return char;
}
function charBack(num) {
    if(num) index -= num - 1;
    char = index > 0 ? dateformat[--index] : null;
    return char;
}
// 如果字元是數字形式,從這裡解析
function matchNum() {
    var type = char;
    var len = getTypeLength(); // 獲取type長度,如YYYY,len為4
    var str = splitStrByLength(len); // 減掉datestr前len個字元,並返回前len個字元
    saveToResult(type, str); // 將擷取到的字串儲存在結果中
}
// 如果字元是詞語形式,從這裡解析
function matchWord() {
    trimHead(); // 這個函式中有多個trimHead,說明如[M:week]可以寫成[ M : week ]的形式
    charNext();
    if(!isType()) {
        throwErr();
    }
    var type = char;
    trimHead();
    match(":");
    trimHead();
    var label = getLabel();
    match("]");
    var str = splitStrByChar(getBreakChar());
    saveToResult(type, getNumByWord(type, label, str));
}

function saveToResult(type, str) {
    //
    types[type].result = parseInt(str);
}

function getTypeLength() {
    var len = dateformat.substring(index - 1).match(char + "+")[0].length;
    charNext(len - 1);
    return len;
}

function isType() {
    //
    return types[char];
}

function getLabel() {
    str = "";
    while(charNext()) {
        if(char == null) throwErr();
        if(char == ']') break; 
        str += char;
    }
    charBack();
    return str;
}

function getBreakChar() {
    //
    return dateformat[index];
}

function splitStrByLength(len) {
    var str = datestr.substring(0, len);
    datestr = datestr.substring(len);
    return str;
}

function splitStrByChar(breakChar) {
    var end = breakChar ? datestr.indexOf(breakChar) : datestr.length;
    var str = datestr.substring(0, end);
    datestr = datestr.substring(end);
    return str;
}

function compareChar() {
    if(char == datestr[0]) {
        datestr = datestr.substring(1);
        return true;
    }else {
        throwErr("未匹配到字元" + char);
    }
}

function trimHead() {
    while(charNext() == " ") {}
    charBack();
}

function match(c) {
    if(charNext() == c) {
        return true;
    }else {
        throwErr("缺少識別符號;" + c);
    }
}

function throwErr(err) {
    if(!err) err = "";
    var str = "出錯位置:" + (index - 1).toString() + " \"" + char + "\"。" + err;
    throw str;
}

function getResult() {
    var json = {};
    for(var i in types) {
        json[i] = types[i].result;
    }
    return json;
}

function parse(datestr, dateformat) {
    init(datestr, dateformat);
    try {
        while(charNext()) {
            if(!char) break;
            if(char == "[") {
                matchWord();
                continue;
            }
            if(!isType()) compareChar();
            else matchNum();
        }
    }catch(err) {
        console.log(err);
    }

    var json = getResult()
    //console.log(json);

    var date = new Date();
    date.setYear(json['Y']);
    date.setMonth(json['M']);
    date.setDate(json['D']);
    date.setHours(json['h']);
    date.setMinutes(json['m']);
    date.setSeconds(json['s']);
    date.setMilliseconds(json['x']);
    return date;
}

Date.prototype.parse = parse;
Date.prototype.format = function (fmt) { //author: meizz ,這個函式來自博友,非本人編寫
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "D+": this.getDate(), //日 
        "h+": this.getHours(), //小時 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "x+": this.getMilliseconds() //毫秒 
    };
    if (/(Y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
//*****************************************************
})();