码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript 时间与时间戳转换

时间:2017-06-13 12:51:26      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:blog   span   datetime   pre   turn   date()   转换   gettime   log   

一、获取当前时间戳

var timestamp = new Date().getTime();
console.log(timestamp);

二、yyyy-MM-dd hh:mm:ss 格式的当前时间

function getdate() {
    var now = new Date();
    var   Y = now.getFullYear();
    var   M = now.getMonth() + 1;
    var   D = now.getDate();
    var   m = M < 10 ? "0" + M : M;
    var   d = D < 10 ? "0" + D : D;
    return Y + "-" + m + "-" + d + " " + now.toTimeString().substr(0, 8);
 }

三、时间转为时间戳

 function gettime(dateTime) {                     
    var time = Date.parse(new Date(dateTime));
    return time;
 }

  var stringTime = ‘2017/06/13 10:22:00‘;
  console.log(stringTime + "的时间戳为:" + gettime(stringTime))

 

 

JavaScript 时间与时间戳转换

标签:blog   span   datetime   pre   turn   date()   转换   gettime   log   

原文地址:http://www.cnblogs.com/xsphehe/p/7000214.html

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