欢迎来到 Renly 的博客园,如果小伙伴们有需要讲解的技术,可以给笔者发消息或邮件留言!【renlywen@foxmail.com】

JS时间和时间戳的转换

时间转为时间戳

timeToTimestamp(time){
    let timestamp = Date.parse(time)
    return timestamp;
}

时间戳转为本地时间

timestampToTime(timestamp){
  let date = new Date(timestamp);
  var YY = date.getFullYear() + '-';
  var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
  var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  return YY + MM + DD +" "+hh + mm + ss;
}

  

时间戳转为UTC时间

timestampToUtc(timestamp){
    let date = new Date(timestamp);
    var YY = date.getUTCFullYear() + '-';
    var MM = (date.getUTCMonth() + 1 < 10 ? '0' + (date.getUTCMonth() + 1) : date.getUTCMonth() + 1) + '-';
    var DD = (date.getUTCDate() < 10 ? '0' + (date.getUTCDate()) : date.getUTCDate());
    var hh = (date.getUTCHours() < 10 ? '0' + date.getUTCHours() : date.getUTCHours()) + ':';
    var mm = (date.getUTCMinutes() < 10 ? '0' + date.getUTCMinutes() : date.getUTCMinutes()) + ':';
    var ss = (date.getUTCSeconds() < 10 ? '0' + date.getUTCSeconds() : date.getUTCSeconds());
    return YY + MM + DD +"T"+hh + mm + ss+".000Z";
}

 

posted @ 2020-07-27 15:27  Renly_wen  阅读(2351)  评论(0编辑  收藏  举报
……