时间的转换

 

  1. function getLocalTime(nS) {
  2.  
    var d = new Date(parseInt(nS)* 1000); //根据时间戳生成的时间对象
  3.  
    var date = (d.getFullYear()) + "-" +
  4.  
    (d.getMonth() + 1) + "-" +
  5.  
    (d.getDate()) + " " +
  6.  
    (d.getHours()) + ":" +
  7.  
    (d.getMinutes()) + ":" +
  8.  
    (d.getSeconds());
  9.  
    return date;
  10.  
    }
  11.  
    document.write(getLocalTime(1552889937));

 

1、当前时间换时间戳

  1.  
    var timestamp = parseInt(new Date().getTime()/1000); // 当前时间戳
  2.  
    document.write(timestamp);

2、当前时间换日期字符串

  1.  
    var now = new Date();
  2.  
    var yy = now.getFullYear(); //年
  3.  
    var mm = now.getMonth() + 1; //月
  4.  
    var dd = now.getDate(); //日
  5.  
    var hh = now.getHours(); //时
  6.  
    var ii = now.getMinutes(); //分
  7.  
    var ss = now.getSeconds(); //秒
  8.  
    var clock = yy + "-";
  9.  
    if(mm < 10) clock += "0";
  10.  
    clock += mm + "-";
  11.  
    if(dd < 10) clock += "0";
  12.  
    clock += dd + " ";
  13.  
    if(hh < 10) clock += "0";
  14.  
    clock += hh + ":";
  15.  
    if (ii < 10) clock += '0';
  16.  
    clock += ii + ":";
  17.  
    if (ss < 10) clock += '0';
  18.  
    clock += ss;
  19.  
    document.write(clock); //获取当前日期

3、日期字符串转时间戳

  1.  
    var date = '2015-03-05 17:59:00.0';
  2.  
    date = date.substring(0,19);
  3.  
    date = date.replace(/-/g,'/');
  4.  
    var timestamp = new Date(date).getTime();
  5.  
    document.write(timestamp);

 

posted @ 2021-01-19 08:33  诡道也  阅读(89)  评论(0编辑  收藏  举报