zoukankan      html  css  js  c++  java
  • JS关于Date函数的格式化输出

    // 对Date的扩展,将 Date 转化为指定格式的String  
    // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,   
    // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)   
    // 例子:   
    // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423   
    // (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18   
      
    Date.prototype.Format = function(fmt){  
        var o = {  
             "M+": this.getMonth()+1,  
             "d+": this.getDate(),  
             "H+": this.getHours(),  
             "m+": this.getMinutes(),  
             "s+": this.getSeconds(),  
             "S+": this.getMilliseconds()  
        };  
      
        //因位date.getFullYear()出来的结果是number类型的,所以为了让结果变成字符串型,下面有两种方法:  
      
          
      
        if(/(y+)/.test(fmt)){  
            //第一种:利用字符串连接符“+”给date.getFullYear()+"",加一个空字符串便可以将number类型转换成字符串。  
      
            fmt=fmt.replace(RegExp.$1,(this.getFullYear()+"").substr(4-RegExp.$1.length));  
        }  
        for(var k in o){  
            if (new RegExp("(" + k +")").test(fmt)){  
      
                //第二种:使用String()类型进行强制数据类型转换String(date.getFullYear()),这种更容易理解。  
      
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(String(o[k]).length)));  
            }  
        }     
        return fmt;  
    } 
    window.onload=function(){  
        var time1 = new Date().Format("yyyy-MM-dd");  
        var time2 = new Date().Format("yyyy-MM-dd HH:mm:ss");   
        alert(time1);  
        alert(time2);  
          
    };  

     格式化时间

        // 格式化日期函数
        function formatDate(v) {
            if (/^(-)?d{1,10}$/.test(v)) {
                v = v * 1000;
            } else if (/^(-)?d{1,13}$/.test(v)) {
                v = v * 1;
            }
            var dateObj = new Date(v);
            var month = dateObj.getMonth() + 1;
            var day =  dateObj.getDate();
            var hours = dateObj.getHours();
            var minutes = dateObj.getMinutes();
            var seconds = dateObj.getSeconds();
            if(month<10){
                month = "0" + month;
            }
            if(day<10){
                day = "0" + day;
            }
            if(hours<10){
                hours = "0" + hours;
            }
            if(minutes<10){
                minutes = "0" + minutes;
            }
            if(seconds<10){
                seconds = "0" + seconds;
            }
            var UnixTimeToDate = dateObj.getFullYear() + '-' + month + '-' +day + ' ' + hours + ':' + minutes + ':' + seconds;
            return UnixTimeToDate;
        }
  • 相关阅读:
    webstorm快捷键大全
    Js的reduce()方法
    利用 c# linq 实现多个数据库的联合查询
    微信小程序支付接入注意点
    ubuntu所有php扩展php-7.0扩展列表
    ubuntu 16.04 php 安装curl方法
    Ubuntu下配置Apache开启HTTPS
    Mac下如何用SSH连接远程Linux服务器及Linux一些常用操作命令,更新中.....
    Mac下如何用SSH连接远程Linux服务器
    C#的dapper使用
  • 原文地址:https://www.cnblogs.com/lguow/p/9209554.html
Copyright © 2011-2022 走看看