从UTC时区确定DST或CST时区

问题描述 投票:0回答:1

我有UTC timeZone日期,我想根据是否在同一城市中应用DST来转换CST或DST。

由于我的浏览器不是基于同一城市,所以当地时间不使用它。例如:我的用户基于印度,但他们还将看到纽约日期的数据]

我有一个基于NewYork的服务器,因此使用SSR可以获取服务器的日期,但是它始终在CST中显示如何将日期更改为DST?

javascript node.js
1个回答
0
投票

尝试此代码。它可能会帮助您。

  let d = new Date()
  let utc = d.getTime() + (d.getTimezoneOffset() * 60000)
  let today = new Date(utc + (3600000 * 6))  // 6 is for cst timezone. we can change this to get other timezone.

  let dd = today.getDate()
  let mm = today.getMonth()+1
  let yy = today.getFullYear()
  let hh = today.getHours()
  let i  = today.getMinutes()
  let sec = today.getSeconds()

  hh = hh.toString().length<2?("0"+hh):hh
  mm = mm.toString().length<2?("0"+mm):mm
  i = i.toString().length<2?"0"+i:i
  sec = sec.toString().length<2?"0"+sec:sec

  let timestamp = dd+'/'+mm+'/'+yy+' '+hh+':'+i+':'+sec
  console.log('timestamp ',timestamp)
© www.soinside.com 2019 - 2024. All rights reserved.