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

时间戳/日期字符串

时间:2019-01-21 13:50:26      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:date   获取   lock   字符   date()   var   sub   int   str   

  1. 当前时间换时间戳。
var timestamp = parseInt(new Date().getTime()/1000);    // 当前时间戳
document.write(timestamp);
  1. 当前时间换日期字符串
var now = new Date();
var yy = now.getFullYear();      //年
var mm = now.getMonth() + 1;     //月
var dd = now.getDate();          //日
var hh = now.getHours();         //时
var ii = now.getMinutes();       //分
var ss = now.getSeconds();       //秒
var clock = yy + "-";
if(mm < 10) clock += "0";
clock += mm + "-";
if(dd < 10) clock += "0";
clock += dd + " ";
if(hh < 10) clock += "0";
clock += hh + ":";
if (ii < 10) clock += ‘0‘; 
clock += ii + ":";
if (ss < 10) clock += ‘0‘; 
clock += ss;
document.write(clock);     //获取当前日期
    
  1. 日期字符串转时间戳
var date = ‘2015-03-05 17:59:00.0‘;
date = date.substring(0,19);    
date = date.replace(/-/g,‘/‘); 
var timestamp = new Date(date).getTime();
document.write(timestamp);
  1. 时间戳转日期字符串
var timestamp = ‘1425553097‘;
var d = new Date(timestamp * 1000);    //根据时间戳生成的时间对象
var date = (d.getFullYear()) + "-" + 
           (d.getMonth() + 1) + "-" +
           (d.getDate()) + " " + 
           (d.getHours()) + ":" + 
           (d.getMinutes()) + ":" + 
           (d.getSeconds());
document.write(date);

时间戳/日期字符串

标签:date   获取   lock   字符   date()   var   sub   int   str   

原文地址:https://www.cnblogs.com/aryu/p/10298020.html

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