• 日期与时间戳之间的转换


    验证时间戳与日期之间的转换:
    
    var timestamp = Date.parse(new Date());   //获取当前时间的时间戳
    timestamp = timestamp / 1000;
    console.log(timestamp);
    
    
    var stringTime1 = "2014-07-10 10:21:12";  //获取固定格式时间的时间戳
    var timestamp1 = Date.parse(new Date(stringTime1));
    timestamp1 = timestamp1 / 1000;
    //2014-07-10 10:21:12的时间戳为:1404958872
    console.log(stringTime1 + "的时间戳为:" + timestamp1);
    
    
    var stringTime2 = "2014-07-10";
    var timestamp2 = Date.parse(new Date(stringTime2));
    timestamp2 = timestamp2 / 1000;
    //2014-07-10 10:21:12的时间戳为:1404958872
    console.log(stringTime2 + "的时间戳为:" + timestamp2);
    
    
    
    // 将当前时间换成时间格式字符串
    var timestamp3 = 1510220036;   
    var newDate = new Date();
    newDate.setTime(timestamp3 * 1000);
    
    Date.prototype.format = function(format) {
        var date = {
            "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+)/i.test(format)) {
            format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
        }
        for (var k in date) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length == 1
                    ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
            }
        }
        return format;
    }
    console.log(newDate.format('yyyy-MM-dd h:m:s'));  

    参考:http://blog.csdn.net/inuyasha1121/article/details/45093797

    验证时间戳与日期之间的转换:


    var timestamp = Date.parse(new Date());   //获取当前时间的时间戳
    timestamp = timestamp / 1000;
    console.log(timestamp);


    var
    stringTime1 = "2014-07-10 10:21:12"//获取固定格式时间的时间戳
    var timestamp1 = Date.parse(new Date(stringTime1));
    timestamp1 = timestamp1 / 1000;
    //2014-07-10 10:21:12的时间戳为:1404958872
    console.log(stringTime1 + "的时间戳为:" + timestamp1);


    var
    stringTime2 = "2014-07-10";
    var
    timestamp2 = Date.parse(new Date(stringTime2));
    timestamp2 = timestamp2 / 1000;
    //2014-07-10 10:21:12的时间戳为:1404958872
    console.log(stringTime2 + "的时间戳为:" + timestamp2);



    // 将当前时间换成时间格式字符串
    var timestamp3 = 1510220036;  
    var
    newDate = new Date();
    newDate.setTime(timestamp3 * 1000);

    Date.prototype.format = function(format) {
       
    var date = {
           
    "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+)/i.test(format)) {
            format = format.
    replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
       
    }
       
    for (var k in date) {
           
    if (new RegExp("(" + k + ")").test(format)) {
                format = format.
    replace(RegExp.$1, RegExp.$1.length == 1
                   
    ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
           
    }
        }
       
    return format;
    }
    console.log(newDate.format('yyyy-MM-dd h:m:s'));

    宝剑锋从磨砺出,梅花香自苦寒来。
  • 相关阅读:
    14.2.2.4 InnoDB Record, Gap, and Next-Key Locks
    Linux_PXE服务器_RHEL7
    Linux_PXE服务器_RHEL7
    Caused by: java.net.SocketException: Connection reset
    mysql read committed
    Linux_OpenSSH远程连接
    Linux_OpenSSH远程连接
    dns nsswitch.conf
    Python基本语法_强制数据类型转换
    Python基本语法_强制数据类型转换
  • 原文地址:https://www.cnblogs.com/haimengqingyuan/p/7811246.html
Copyright © 2020-2023  润新知