美文网首页
js,时间,日期转换

js,时间,日期转换

作者: 考拉_2044 | 来源:发表于2021-01-25 17:32 被阅读0次
   // 时间转化为时间戳 11:11
    getMillisecond(time) {
      const timeArray = time.split(':');
      return timeArray[0] * 3600000 + timeArray[1] * 60000;
    },
    // 时间戳转化为时间
    dateFormat(fmt, date) {
      let ret;
      const utc = date.getTimezoneOffset() / 60;
      const opt = {
        'Y+': date.getFullYear().toString(), // 年
        'm+': (date.getMonth() + 1).toString(), // 月
        'd+': date.getDate().toString(), // 日
        "H+": date.getHours().toString(),           // 时
        //'H+': (date.getHours() + utc < 0 ? 24 + date.getHours() + utc : date.getHours() + utc).toString(), // 时(转UTC时间)
        'M+': date.getMinutes().toString(), // 分
        'S+': date.getSeconds().toString() // 秒
        // 有其他格式化字符需求可以继续添加,必须转化成字符串
      };
      for (const k in opt) {
        ret = new RegExp('(' + k + ')').exec(fmt);
        if (ret) {
          fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, '0')));
        }
      }
      return fmt;
    }

dateFormat("HH:MM",new Date(xxxxxxx))
// 获取UTC 时区
function timeZoneValue(from) {
  const d = String(new Date()).split(' ');
  return d[5];
};

相关文章

网友评论

      本文标题:js,时间,日期转换

      本文链接:https://www.haomeiwen.com/subject/xvjizktx.html