时间格式和时间戳的转换

//获取当前时间戳(精确到秒)
let nowTime = Date.parse(new Date());
//获取当前时间戳(精确到毫秒)
let nowTime =new Date().getTime();
let nowTime =(new Date()).valueOf();
//获取当前时间( 年-月-日 时:分:秒)
function getNowFormatDate() {
  var date = new Date();
  var seperator1 = "-";
  var seperator2 = ":";
  var month = date.getMonth() + 1<10? "0"+(date.getMonth() + 1):date.getMonth() + 1;
  var strDate = date.getDate()<10? "0" + date.getDate():date.getDate();
  var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
    + " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds();
  return currentdate;
}
//将 年-月-日 时:分:秒时间格式转换为时间戳
var startDate = 2020-04-28 17:50:50
startDate = startDate.replace(/-/g, "/");
var time = new Date(startDate);
time = time.getTime();

 

posted @ 2020-04-28 17:53  静享华年  阅读(403)  评论(0编辑  收藏  举报