注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Computer Science

I Pressed My Words Here. Will You Read ?

 
 
 

日志

 
 

JavaScript之日期偏移  

2012-08-23 15:20:50|  分类: My Projects |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
引用文章:
JavaScript格式化日期时间,仅需三行代码 http://www.veryhuo.com/a/view/38913.html
参考文章:
JavaScript自定义日期格式化函数 http://blog.csdn.net/zhangxin09/article/details/6111294
Javascript的Date格式化方法的增强 http://fluagen.blog.51cto.com/146595/247826
ISO 8601 http://en.wikipedia.org.yi.mk.gd/wiki/ISO_8601
ISO 8601 http://zh.wikipedia.org/w/index.php?title=ISO_8601&_=20120823050803
日期和时间格式 http://msdn.microsoft.com/zh-cn/library/ms141005%28v=sql.90%29.aspx

通过日期对象获取人性化的文本字符串:
if (!Date.prototype.getString) {
        Date.prototype.getString = function(y, m, d, formatStr) {
                void function() {
                        if (y === undefined{
                                y = 0;
                        }
                        if (m === undefined{
                                m = 0;
                        }
                        if (d === undefined{
                                d = 0;
                        }
                        if (formatStr === undefined{
                                formatStr = 'yyyy-MM-dd';
                        }
                } ();
                var tempDate = new Date(this.getFullYear() + y, this.getMonth() + m, this.getDate() + d);
                return tempDate.format(formatStr);
        }
}

从网上找到的,利用正则表达式格式化Date的代码逻辑:
if (!Date.prototype.format) {
        Date.prototype.format = function(formatStr) {
                //代码来自:JavaScript格式化日期时间,仅需三行代码 http://www.veryhuo.com/a/view/38913.html
                var x = this;
                var y = formatStr;
                var z = {
                        M: x.getMonth() + 1
                        ,d: x.getDate()
                        ,h: x.getHours()
                        ,m: x.getMinutes()
                        ,s: x.getSeconds()
                };
                y = y.replace(/(M+|d+|h+|m+|s+)/g, function(v) {
                        return ((v.length > 1 ? "0": "") + eval('z.' + v.slice( - 1))).slice( - 2)
                });
                return y.replace(/(y+)/g, function(v) {
                        return x.getFullYear().toString().slice( - v.length)
                });
                //代码来自:JavaScript格式化日期时间,仅需三行代码 http://www.veryhuo.com/a/view/38913.html
        }
}

读完以上代码后,有一事不解:
yyyy-MM-dd 为何MM要大写,而dd不能大写?
其大小写的含义分别是什么?
另外,见到在 yyyy-MM-dd 这样的表达上各个语言不统一,能像正则一样尽量统一多好的。

后来google到了ISO 8601规范,于是,问题便解决了大半。

根据ISO 8601 (2004)规范(详见参考文章)更改的逻辑:
// 根据 ISO 8601 改良后的代码
if (!Date.prototype.format) {
        Date.prototype.format = function(formatStr) {
                var x = this;
                var y = formatStr;
                var pt = 'P';
                var z = {
                        P: {
                                m: x.getMonth() + 1
                                ,M: x.getMonth() + 1
                                ,d: x.getDate()
                                ,D: x.getDate()
                                ,h: x.getHours()
                                ,H: x.getHours()
                        }
                        ,T: {
                                m: x.getMinutes()
                                ,M: x.getMinutes()
                                ,s: x.getSeconds()
                                ,S: x.getSeconds()
                        }
                };
                y = y.replace(/(P+|T+|M+|d+|h+|m+|s+)/g, function(v) {
                        switch (v) {
                        case 'P':
                                pt = 'P';
                                return '';
                                break;
                        case 'T':
                                pt = 'T';
                                return '';
                                break;
                        default:
                                break;
                        }
                        return ((v.length > 1 ? "0": "") + eval('z.' + pt + '.' + v.slice( - 1))).slice( - 2)
                });
                return y.replace(/(y+)/g, function(v) {
                        return x.getFullYear().toString().slice( - v.length)
                });
        }
}


有更加了解的欢迎回复,指教。



-----------------------------------------
2012-9-12 09:39:47 补充:
如果觉得Date的getString的入参太过繁琐,如果偏移量为日,还得将年月变量写入。
那么,还有种入参设计方式,这是我以前写的一个函数(非Date类型原生方法扩展)
getDateStr(type, num, dateStr). 调用时,能嵌套调用:getDateStr('y',1,getDateStr('d',1)); //得到一年后的下一天。


-----------------------------------------
2014-09-30 10:56
refer to : http://www.w3.org/TR/NOTE-datetime
   Year:
      YYYY (eg 1997)
   Year and month:
      YYYY-MM (eg 1997-07)
   Complete date:
      YYYY-MM-DD (eg 1997-07-16)
   Complete date plus hours and minutes:
      YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
   Complete date plus hours, minutes and seconds:
      YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
   Complete date plus hours, minutes, seconds and a decimal fraction of a
second
      YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)

where:

     YYYY = four-digit year
     MM   = two-digit month (01=January, etc.)
     DD   = two-digit day of month (01 through 31)
     hh   = two digits of hour (00 through 23) (am/pm NOT allowed)
     mm   = two digits of minute (00 through 59)
     ss   = two digits of second (00 through 59)
     s    = one or more digits representing a decimal fraction of a second
     TZD  = time zone designator (Z or +hh:mm or -hh:mm)
------------------------------------------------------------------
2014-09-30 10:56
根据ISO 8601 (2004)规范(详见参考文章)更改的逻辑:
// 根据 ISO 8601 改良后的代码
// e.g: Date.format("PYYYY-MM-DD Thh:mm:ss");
if (!Date.prototype.format) {
        Date.prototype.format = function(formatStr) {
                var x = this;
                var y = formatStr;
                var pt = 'P';
                var z = {
                        P: {
                                m: x.getMonth() + 1
                                ,M: x.getMonth() + 1
                                ,d: x.getDate()
                                ,D: x.getDate()
                        }
                        ,T: {
                                h: x.getHours()
                                ,H: x.getHours()
                                ,m: x.getMinutes()
                                ,M: x.getMinutes()
                                ,s: x.getSeconds()
                                ,S: x.getSeconds()
                        }
                };
                y = y.replace(/(P+|T+|M+|D+|h+|m+|s+)/gfunction(v) {
                        switch (v) {
                        case 'P':
                                pt = 'P';
                                return '';
                                break;
                        case 'T':
                                pt = 'T';
                                return '';
                                break;
                        default:
                                break;
                        }
                        return ((v.length > 1 ? "0""") + eval('z.' + pt + '.' + v.slice( - 1))).slice( - 2)
                });
                return y.replace(/(Y+)/gfunction(v) {
                        return x.getFullYear().toString().slice( - v.length)
                });
        }
}




if (!Date.prototype.getString) {
        Date.prototype.getString = function(y, m, d, formatStr) {
                void function() {
                        if (y === undefined{
                                y = 0;
                        }
                        if (m === undefined{
                                m = 0;
                        }
                        if (d === undefined{
                                d = 0;
                        }
                        if (formatStr === undefined{
                                formatStr = 'yyyy-MM-dd';
                        }
                } ();
                var tempDate = new Date(this.getFullYear() + y, this.getMonth() + m, this.getDate() + d);
                return tempDate.format(formatStr);
        }
}


if (!Date.prototype.GetStr){
        Date.prototype.GetStr = function (num, type, formatStr, DateStr){
                var date = (undefined != DateStr && DateStr != "") ? new Date(DateStr.replace(/-/g,"/")): this;
                var Y = 0, M = 0, D = 0, h =0 , m =0, s =0;
                switch(type){
                        case "Y": Y = num; break;
                        case "M": M = num; break;
                        case "D": D = num; break;
                        case "h": h = num; break;
                        case "m": m = num; break;
                        case "s": s = num; break;
                        default: d = num; break;
                }
                formatStr = (undefined == formatStr || formatStr == "") ? "YYYY-MM-DD": formatStr;
                var tempDate = new Date(date.getFullYear() + Y , date.getMonth() + M, date.getDate() + D, date.getHours() + h, date.getMinutes() + m, date.getSeconds() + s);
                return tempDate.format(formatStr);
        }
}

/*TODO: firstWeekDate of the month*/
if(!Date.prototype.getFirstWeekDateOftheMonth){
        Date.prototype.getFirstWeekDateOftheMonth = function(){
                return Date(this.GetStr(1-this.getDate(),'d','yyyy/MM/dd')).getDay();
        }
}
/*TODO: */
  评论这张
 
阅读(1048)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017