zoukankan      html  css  js  c++  java
  • 6.js 获取当前日期时间3种格式化方法

    js 获取当前日期时间3种格式化方法 yyyy-mm-dd hh:MM:ss

     原文地址:https://www.cnblogs.com/3box/p/5748007.html

    方法一:

    复制代码
    Date.prototype.format = function (format) {
               var args = {
                   "M+": this.getMonth() + 1,
                   "d+": this.getDate(),
                   "h+": this.getHours(),
                   "m+": this.getMinutes(),
                   "s+": this.getSeconds(),
                   "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
                   "S": this.getMilliseconds()
               };
               if (/(y+)/.test(format))
                   format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
               for (var i in args) {
                   var n = args[i];
                   if (new RegExp("(" + i + ")").test(format))
                       format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? n : ("00" + n).substr(("" + n).length));
               }
               return format;
           };
    
    调用方法
    
    alert(new Date().format("yyyy-MM-dd hh:mm:ss:S"));
    
    alert(new Date().format("yyyy-MM-dd hh:mm:ss"));
    复制代码

    ------------------------------------------------------------------------------------

    方法二:

    复制代码
    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var seperator2 = ":";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = date.getYear() + seperator1 + month + seperator1 + strDate
                + " " + date.getHours() + seperator2 + date.getMinutes()
                + seperator2 + date.getSeconds();
        return currentdate;
    }
    复制代码

    ------------------------------------------------------------------------------------

    方法三:

    复制代码
    function curDateTime(){
    var d = new Date(); 
    var year = d.getYear(); 
    var month = d.getMonth()+1; 
    var date = d.getDate(); 
    var day = d.getDay(); 
    var hours = d.getHours(); 
    var minutes = d.getMinutes(); 
    var seconds = d.getSeconds(); 
    var ms = d.getMilliseconds(); 
    var curDateTime= year;
    if(month>9)
    curDateTime = curDateTime +"-"+month;
    else
    curDateTime = curDateTime +"-0"+month;
    if(date>9)
    curDateTime = curDateTime +"-"+date;
    else
    curDateTime = curDateTime +"-0"+date;
    if(hours>9)
    curDateTime = curDateTime +""+hours;
    else
    curDateTime = curDateTime +"0"+hours;
    if(minutes>9)
    curDateTime = curDateTime +":"+minutes;
    else
    curDateTime = curDateTime +":0"+minutes;
    if(seconds>9)
    curDateTime = curDateTime +":"+seconds;
    else
    curDateTime = curDateTime +":0"+seconds;
    return curDateTime; 
    }
    
    alert(curDateTime());
     
  • 相关阅读:
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    Oracle数据库基础
    软件项目开发模式——三层模式
    JavaWeb——Ajax与MVC学习总结
    JavaWeb——EL及JSTL学习总结
    JavaWeb——过滤器及监听器
    JavaWeb——Servlet开发
  • 原文地址:https://www.cnblogs.com/Nick-Hu/p/8334442.html
Copyright © 2011-2022 走看看