• javascript中Date的扩展方法


    今天在网上摘抄了些js中操作日期的相关方法,现在与大家分享一下。

    <script type="text/javascript">
        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), //季度 
                "S" : 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; 
        }

        Date.prototype.addDays 
    = function(d)
        {
            
    this.setDate(this.getDate() + d);
        };

        Date.prototype.addWeeks 
    = function(w)
        {
            
    this.addDays(w * 7);
        };

        Date.prototype.addMonths
    = function(m)
        {
            
    var d = this.getDate();
            
    this.setMonth(this.getMonth() + m);

            
    if (this.getDate() < d)
                
    this.setDate(0);
        };

        Date.prototype.addYears 
    = function(y)
        {
            
    var m = this.getMonth();
            
    this.setFullYear(this.getFullYear() + y);

            
    if (m < this.getMonth()) 
            {
                
    this.setDate(0);
            }
        };
        
    //测试 var now = new Date(); now.addDays(1);//加减日期操作 alert(now.Format("yyyy-MM-dd"));

        Date.prototype.dateDiff 
    = function(interval,endTime)  
        {      
            
    switch (interval)      
            {             
                
    case "s":   //計算秒差                        
                    return parseInt((endTime-this)/1000);                                  
                case "n":   //計算分差                    
                    return parseInt((endTime-this)/60000);               
                case "h":   //計算時差                        
                    return parseInt((endTime-this)/3600000);                 
                case "d":   //計算日差             
                    return parseInt((endTime-this)/86400000);            
                case "w":   //計算週差            
                    return parseInt((endTime-this)/(86400000*7));            
                case "m":   //計算月差                 
                    return (endTime.getMonth()+1)+((endTime.getFullYear()-this.getFullYear())*12)-(this.getMonth()+1);           
                
    case "y":   //計算年差                       
                    return endTime.getFullYear()-this.getFullYear();            
                
    default:    //輸入有誤                      
                    return undefined;       
            }    
        } 
        
    //测试 var starTime = new Date("2007/05/12 07:30:00");     var endTime = new Date("2008/06/12 08:32:02");     document.writeln("秒差: "+starTime .dateDiff("s",endTime )+"<br>");     document.writeln("分差: "+starTime .dateDiff("n",endTime )+"<br>");     document.writeln("時差: "+starTime .dateDiff("h",endTime )+"<br>");     document.writeln("日差: "+starTime .dateDiff("d",endTime )+"<br>");     document.writeln("週差: "+starTime .dateDiff("w",endTime )+"<br>");     document.writeln("月差: "+starTime .dateDiff("m",endTime )+"<br>");     document.writeln("年差: "+starTime .dateDiff("y",endTime )+"<br>"); 
    </script>
  • 相关阅读:
    【nodejs原理&源码杂记(8)】Timer模块与基于二叉堆的定时器
    【nodejs原理&源码赏析(7)】【译】Node.js中的事件循环,定时器和process.nextTick
    【nodejs原理&源码赏析(6)】深度剖析cluster模块源码与node.js多进程(下)
    【nodejs原理&源码赏析(5)】net模块与通讯的实现
    【nodejs原理&源码赏析(4)】深度剖析cluster模块源码与node.js多进程(上)
    工作一年多的感慨与总结(二)
    工作一年多的感慨与总结(一)
    动手实践Mybatis插件
    MySQL存储引擎
    Tomcat类加载架构
  • 原文地址:https://www.cnblogs.com/linyechengwei/p/1602478.html
Copyright © 2020-2023  润新知