zoukankan      html  css  js  c++  java
  • 时间对象的封装

    <script type="text/javascript">  
     
        //时间对象封装
        function myDate2(date){
            this.date = new Date(date);
            this.Y = function(){
                return this.date.getFullYear();
            }
            this.m = function(){
                var m = (this.date.getMonth()+1).toString();
                if(m.length==1) return "0"+m;
                return m;
            }
            this.d = function(){
                var d = this.date.getDate().toString();
                if(d.length==1) return "0"+d;
                return d;
            }
        }
     
        //时间对象格式化
        myDate2.prototype.Format = function(format){
            if(format=="yyyy"){
                return this.Y();
            }else if(format=="yyyy-mm"){
                return this.Y()+"-"+this.m();
            }else if(format=="yyyy-mm-dd"){
                return this.Y()+"-"+this.m()+"-"+this.d();
            }
            throw new error("非法的格式");
        }
     
        //时间对象加减
        myDate2.prototype.AddDate = function(Y,m,d){
            this.date.setFullYear(parseInt(this.Y())+Y);
            this.date.setMonth(parseInt(this.m())+m-1);
            this.date.setDate(parseInt(this.d())+d);
            return this;
        }
     
        //工厂
        function myDate(date){
            return new myDate2(date);
        }
     
        //举个栗子
        var d = myDate("2016-02-14");//将字符串时间实例化myDate2对象
        d.AddDate(0,0,10);//加10天
        console.log(d.Format("yyyy-mm-dd"));//格式化输出2016-02-24
    </script>
  • 相关阅读:
    I NEED A OFFER!
    水题 Codeforces Round #303 (Div. 2) A. Toy Cars
    模拟 HDOJ 5099 Comparison of Android versions
    模拟 HDOJ 5095 Linearization of the kernel functions in SVM
    贪心 HDOJ 5090 Game with Pearls
    Kruskal HDOJ 1863 畅通工程
    Kruskal HDOJ 1233 还是畅通工程
    并查集 HDOJ 1232 畅通工程
    DFS/并查集 Codeforces Round #286 (Div. 2) B
    水题 Codeforces Round #286 (Div. 2) A Mr. Kitayuta's Gift
  • 原文地址:https://www.cnblogs.com/chengqiaoli/p/5344053.html
Copyright © 2011-2022 走看看