时区之时间戳

间戳是一段记录秒数的整数,它表示从1970年1月1日00:00:00 UTC开始至今的秒数(或毫秒数)

任何时区的时间戳都是相同

 获取当前时区:

new Date().getTimezoneOffset()/-60(60),

中国属于东8区

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function formatTime(date) {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();
  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':');
}
 
const formatNumber = (n) => {
  const str = n.toString();
  return str[1] ? str : '0' + str;
};

 

复制代码
*******/
function getLocalTime(i, timeVal) {
  //参数i为时区值数字,比如北京为东八区则输进8,西5输入-5
  if (typeof i !== 'number') return;
  var d = new Date(timeVal);
  //得到1970年一月一日到现在的秒数
  var len = timeVal;
  //本地时间与GMT时间的时间偏移差
  var offset = d.getTimezoneOffset() * 60000;
  //得到现在的格林尼治时间
  var utcTime = len + offset;
  return formatTime(new Date(utcTime + 3600000 * i));
}
复制代码

 

posted @   小小强学习网  阅读(117)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2022-05-25 React 复制到粘贴板 兼容各种浏览器 copy-to-clipboard、原生js、clipboard方案
2021-05-25 vue-awesome-swiper
点击右上角即可分享
微信分享提示