【问题标题】:I want to get New York time in this JavaScript clock?我想在这个 JavaScript 时钟中获取纽约时间?
【发布时间】:2026-01-20 06:50:01
【问题描述】:

我想在这个 JavaScript 中获取纽约时间。我试过toLocaleTimeString('en-US', { timeZone: 'America/New_York' })

但它不起作用。

Javascript:

setInterval(setClock, 1000)

const hourHandTK = document.querySelector('[data-hour-hand-tk]')
const minuteHandTK = document.querySelector('[data-minute-hand-tk]')
const secondHandTK = document.querySelector('[data-second-hand-tk]')

function setClock() {

var currentDate = new Date()
  const secondsRatio = currentDate.getSeconds() / 60
  const minutesRatio = (secondsRatio + currentDate.getMinutes()) / 60
  const hoursRatio = (minutesRatio + currentDate.getHours()) / 12
  setRotation(secondHandTK, secondsRatio)
  setRotation(minuteHandTK, minutesRatio)
  setRotation(hourHandTK, hoursRatio)
}

function setRotation(element, rotationRatio) {
  element.style.setProperty('--rotation', rotationRatio * 360)
}

setClock()

完整代码:

setInterval(setClock, 1000)

const hourHand = document.querySelector('[data-hour-hand]')
const minuteHand = document.querySelector('[data-minute-hand]')
const secondHand = document.querySelector('[data-second-hand]')

function setClock() {
  const currentDate = new Date()
  const secondsRatio = currentDate.getSeconds() / 60
  const minutesRatio = (secondsRatio + currentDate.getMinutes()) / 60
  const hoursRatio = (minutesRatio + currentDate.getHours()) / 12
  setRotation(secondHand, secondsRatio)
  setRotation(minuteHand, minutesRatio)
  setRotation(hourHand, hoursRatio)
}

function setRotation(element, rotationRatio) {
  element.style.setProperty('--rotation', rotationRatio * 360)
}

setClock()
*, *::after, *::before {
  box-sizing: border-box;
}

body {
  background: linear-gradient(to right, hsl(200, 100%, 50%), hsl(175, 100%, 50%));
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  overflow: hidden;
}

.clock {
  width: 300px;
  height: 300px;
  background-color: rgba(255, 255, 255, .8);
  border-radius: 50%;
  border: 2px solid black;
  position: relative;
}

.clock .number {
  --rotation: 0;
  position: absolute;
  width: 100%;
  height: 100%;
  text-align: center;
  transform: rotate(var(--rotation));
  font-size: 1.5rem;
}

.clock .number1 { --rotation: 30deg; }
.clock .number2 { --rotation: 60deg; }
.clock .number3 { --rotation: 90deg; }
.clock .number4 { --rotation: 120deg; }
.clock .number5 { --rotation: 150deg; }
.clock .number6 { --rotation: 180deg; }
.clock .number7 { --rotation: 210deg; }
.clock .number8 { --rotation: 240deg; }
.clock .number9 { --rotation: 270deg; }
.clock .number10 { --rotation: 300deg; }
.clock .number11 { --rotation: 330deg; }

.clock .hand {
  --rotation: 0;
  position: absolute;
  bottom: 50%;
  left: 50%;
  border: 1px solid white;
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
  transform-origin: bottom;
  z-index: 10;
  transform: translateX(-50%) rotate(calc(var(--rotation) * 1deg));
}

.clock::after {
  content: '';
  position: absolute;
  background-color: black;
  z-index: 11;
  width: 15px;
  height: 15px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
}

.clock .hand.second {
  width: 3px;
  height: 45%;
  background-color: red;
}

.clock .hand.minute {
  width: 7px;
  height: 40%;
  background-color: black;
}

.clock .hand.hour {
  width: 10px;
  height: 35%;
  background-color: black;
}














/* Background Styles Only */

@import url('https://fonts.googleapis.com/css?family=Raleway');

* {
    font-family: Raleway;
}

.side-links {
  position: absolute;
  top: 15px;
  right: 15px;
}

.side-link {
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  margin-bottom: 10px;
  color: white;
  width: 180px;
  padding: 10px 0;
  border-radius: 10px;
}

.side-link-youtube {
  background-color: red;
}

.side-link-twitter {
  background-color: #1DA1F2;
}

.side-link-github {
  background-color: #6e5494;
}

.side-link-text {
  margin-left: 10px;
  font-size: 18px;
}

.side-link-icon {
  color: white;
  font-size: 30px;
}
<div class="clock">
  <div class="hand hour" data-hour-hand></div>
  <div class="hand minute" data-minute-hand></div>
  <div class="hand second" data-second-hand></div>
  <div class="number number1">1</div>
  <div class="number number2">2</div>
  <div class="number number3">3</div>
  <div class="number number4">4</div>
  <div class="number number5">5</div>
  <div class="number number6">6</div>
  <div class="number number7">7</div>
  <div class="number number8">8</div>
  <div class="number number9">9</div>
  <div class="number number10">10</div>
  <div class="number number11">11</div>
  <div class="number number12">12</div>
</div>

<!-- Side Links Only -->
<div class="side-links">
  <a href="https://youtu.be/Ki0XXrlKlHY" target="_blank" class="side-link side-link-youtube">
    <i class="fab fa-youtube-square side-link-icon"></i>
    <span class="side-link-text">View Tutorial</span>
  </a>
  <a href="https://github.com/WebDevSimplified" target="_blank" class="side-link side-link-github side-link-icon">
    <i class="fab fa-github-square"></i>
    <span class="side-link-text">View GitHub</span>
  </a>
  <a href="https://twitter.com/DevSimplified" target="_blank" class="side-link side-link-twitter">
    <i class="fab fa-twitter-square side-link-icon"></i>
    <span class="side-link-text">View Twitter</span>
  </a>
</div>

提前致谢。

【问题讨论】:

标签: javascript


【解决方案1】:

toLocaleTimeString 将您的日期转换为字符串。

这应该可行

const currentDate = new Date(new Date().toLocaleString("en-US", {timeZone: "America/New_York"}));

【讨论】:

  • 我试过了,但没用。感谢您的评论。
  • 它对我来说很好用。我在 CET 时区的 15:32 运行它,返回的 Date 对象显示 09:32(这是纽约的当前时间)。它以何种方式对您不起作用?您在哪个浏览器中测试它(很确定这在 Internet Explorer 中不起作用)?
  • 我在 chrome 和 firefox 开发者版中试过,但没有用。
  • 谢谢,已经成功了。它不适用于可能用于 cookie 的浏览器。非常感谢。
【解决方案2】:

当然toLocaleTimeString('en-US', { timeZone: 'America/New_York' }) 不会工作,因为它将日期的时间部分作为字符串返回(例如:9:19:06 AM

在你的情况下,我想这将是解决方案:

var currentDate = new Date(
    (new Date()).toLocaleString(
        'en-US',
        { timeZone: 'America/New_York' }
    )
)

【讨论】:

  • 谢谢,这对我有用。
  • @ChowLip 欢迎您!不要忘记投赞成票;)
【解决方案3】:

如果你愿意,这是你的选择,但如果你愿意,尝试使用nodejs,安装后你可以安装https://www.npmjs.com/package/moment,然后你需要的唯一代码是

var moment = require("moment-timezone")

var june = moment(new Date());
june.tz('America/New_York').format('ha z');

如果您愿意,我也可以帮助您完成安装过程。

【讨论】:

  • 感谢您的回复。 var currentDate = new Date( (new Date()).toLocaleString( 'en-US', { timeZone: 'America/New_York' } ) ) 为我工作。
最近更新 更多