码迷,mamicode.com
首页 > 其他好文 > 详细

日期与时间戳之间的转换

时间:2017-11-09 22:39:56      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:log   detail   字符   当前时间   获取   cti   mes   mil   bst   

 

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

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‘));

日期与时间戳之间的转换

标签:log   detail   字符   当前时间   获取   cti   mes   mil   bst   

原文地址:http://www.cnblogs.com/haimengqingyuan/p/7811246.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!