时间戳、中国标准时间、年月日三种时间格式转换

以2022年4月9号为例,列出三种时间格式形式:
时间戳-格式: 1649462400000
中国标准时间-格式: Sat Apr 09 2022 08:00:00 GMT+0800 (中国标准时间)
年-月-日-格式: 2022-04-09

时间戳 转换成 中国标准时间

function getSimpleDate(date) {
  let dd = new Date(date) // 时间戳转化成中国标准时间格式
  return dd 
}

中国标准时间 转换成 年月日

// 中国标准时间 转换成 年月日
function formatDateTime (date) {
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  var d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  var h = date.getHours();
  var minute = date.getMinutes();
  minute = minute < 10 ? ('0' + minute) : minute;
  // return y + '-' + m + '-' + d+' '+h+':'+minute;
  return y + '-' + m + '-' + d
};

年月日转换成 时间戳

function getStampDate(date) {
  let stamp = new Date(currentDate).getTime() // 年月日转化成时间戳
  //let preDate = new Date(currentDate).getTime() - (24 * 60 * 60 * 1000) // 前一天时间戳
  //let afterDate = new Date(currentDate).getTime() + (24 * 60 * 60 * 1000) // 后一天时间戳
  return stamp
}

备注:用时间戳来计算前一天后和后一天不会出(29号、30号、31号)的误判

猜你喜欢

转载自blog.csdn.net/weixin_42132841/article/details/128142666
今日推荐