首页 > 网站 > WEB开发 > 正文

给javascript的Date添加parse和format函数

2024-04-27 15:14:22
字体:
来源:转载
供稿:网友

前端开发中遇到日期字符串的解析和格式化问题,为了满足更多情况,于是花心思自己写了一个版本,适用较多情况,如下:

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 982632988@QQ.com    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;}//*****************************************************})();


上一篇:js闭包的理解

下一篇:WebSocket简单应用

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表