zoukankan      html  css  js  c++  java
  • 将new Date() 格式化为 ’2018-10-11‘ 的字符串格式

    function dateToString( date , format ){
            if(!date) return "";
            if (!Common.type.isDate(date)) return ""; //详见博客:常用类型判断
            format = format ? format : "y-m-d";
            switch(format){
                case "y-m":
                    return date.getFullYear() + "-" + datePad( date.getMonth() + 1, 2 );
                case "y-m-d":
                    return date.getFullYear() + "-" + datePad( date.getMonth() + 1, 2 ) + "-" + datePad( date.getDate(), 2 );
                case "h-m-s":
                    return datePad( date.getHours(), 2 ) + ":" + datePad( date.getMinutes(), 2 ) + ":" + datePad( date.getSeconds(), 2);
                case "y-m-d-h-m-s":
                    return date.getFullYear() + "-" + datePad( date.getMonth() + 1, 2 ) + "-" + datePad( date.getDate(), 2 ) + " " + datePad( date.getHours(), 2 ) + ":" + datePad( date.getMinutes(), 2 ) + ":" + datePad( date.getSeconds(), 2);
            }
        }

     function datePad(num, n){  
            if( ( num + "" ).length >= n )
            return num; //一位数
            return arguments.callee( "0" + num, n ); //两位数
         }

     Common.type.isDate: function() {
            for (var b = 0, a = arguments.length; b < a; b++) {
                o = arguments[b];
                if (!(System.type.isObject(o) && o.constructor && (o.constructor.toString().indexOf("Date") > -1 || o instanceof Date))) {
                    return false;
                }
            }
            return true;
         }

       例:

          var today = new Date();

        dateToString(today);  // '2017-10-11'

          dateToString(today,'y-m');  //'2017-10'

          dateToString(today,'y-m-d-h-m-s');  //'2017-10-11 16:42:59'

  • 相关阅读:
    Python中的sorted函数以及operator.itemgetter函数
    a=a+(a++);b=b+(++b);计算顺序,反汇编
    带基虚类的构造函数执行顺序
    开源系统管理资源大合辑
    linux的LNMP架构介绍、MySQL安装、PHP安装
    lamp下mysql安全加固
    ITSS相关的名词解释
    从苦逼到牛逼,详解Linux运维工程师的打怪升级之路
    Linux 文件系统概览
    Exchange2010批量删除邮件
  • 原文地址:https://www.cnblogs.com/cryst/p/7651737.html
Copyright © 2011-2022 走看看