js,获取其他时区的午夜当地时间 [英] js, get the local time at midnight for a different timezone

查看:82
本文介绍了js,获取其他时区的午夜当地时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算第二天在洛杉矶的当前本地时间。目的是将cookie设置到la的下一个午夜。

I would like to calculate the current local time when it's 24:00 the next day in Los Angeles. The purpose is to set a cookie until the next midnight in la.

我使用了时区和时区,但是,我无法证明构建大小的开销是合理的

I have used moment and moment timezone, however, I can't justify the overhead to the build size for a single function.

这是我正在使用的功能,但是我仍然坚持将LA午夜的时间转换回本地时间,就像我转换回去的那样。在当前时间之前。我认为反转时 convertToOtherTimezone 函数不会有问题,但我不太确定还有什么要探索的。

Here is what I am working with but I'm stuck on converting the time from LA midnight back to the local time as when i convert back it's before the current time. I don't think there will be an issue with the convertToOtherTimezone function when reversing but I'm not too sure what else to explore.

非常感谢您的帮助。

const convertToOtherTimezone = (date, from, to) => {
  const utc = date.getTime() + (3600000 * from)
  return new Date(utc + (3600000 * to))
}

console.log(
  'native local time in LA',
  new Date().toLocaleString('en-US', { timeZone: 'America/Los_Angeles' })
)

const LA_OFFSET = -7
// I'm using the AEST timezone -10
const LOCAL_OFFSET = new Date().getTimezoneOffset() / 60

const midnightInLA = (localOffset) => {
  // get the current time in LA
  const la = convertToOtherTimezone(new Date(), localOffset, LA_OFFSET)
  console.log('current time in LA', la)
  // set date to midnight in LA's timezone
  la.setHours(24,0,0,0)
  console.log('next midnight in LA', la)
  // convert back to local time
  return convertToOtherTimezone(la, LA_OFFSET, localOffset)
  // Ulias Hunka gave the correct answer, but deleted his post.
  // reverse the offsets
  return convertToOtherTimezone(la, LA_OFFSET * -1, localOffset * -1)
}

console.log(
  'la midnight in local time',
  midnightInLA(LOCAL_OFFSET)
)

<script src="https://codepen.io/synthet1c/pen/KyQQmL.js"></script>

推荐答案

<洛杉矶的标准时间是UTC-0800。当偏移量更改为UTC-0700时,它将在三月的第二个星期日凌晨2点更改为夏令时(DST)。它在11月的第一个星期日的凌晨2点(DST)结束。

Los Angeles standard time is UTC-0800. It changes into daylight saving time (DST) at 2am on the second Sunday in March when the offset changes to UTC-0700. It ends at 2am (DST) on the first Sunday in November.

这些规则很可能会在将来持续一段时间,如果您只是对当前日期感兴趣,您可以使用这些规则,直到它们更改为止。您可以计算给定日期和时间的偏移量,然后为洛杉矶的下一个午夜创建日期。我希望您已将该信息放在问题中而不是在评论中。见下文。

It is likely that these rules will persist for some time into the future, and if you're only interested in present dates, you can use these rules until they change. You can work out the offset for a given date and time, then create a date for the next midnight in LA. I wish you had put that information in the question and not in comments. See below.

/**
 *  Calculate the offset in LA for the given date and time.
 *  LA DST starts on the second Sunday in March at
 *  10:00 UTC. After that, the offset is UTC-0700
 *  LA DST ends on the first Sunday in November at 09:00
 *  UTC. After that the offset is UTC-0800
 *
 *  @param {Date} date - date object
 *  @returns (boolean} true if DST is being observed
 */
function getLAOffset(date) {
  // Get DST start in UTC
  var start = new Date(Date.UTC(date.getUTCFullYear(), 2, 1, 10));
  start.setUTCDate(start.getUTCDate() + (7-start.getUTCDay())%7 + 7);
  // Get DST end in UTC
  var end = new Date(Date.UTC(date.getUTCFullYear(), 10, 1, 9));
  end.setUTCDate(end.getUTCDate() + (7-end.getUTCDay())%7);
  return (date >= start && date < end)? -7 : -8;
}

/** Return a Date object set to midnight in LA
 *  for the next midnight after the given date.
 *  Offset comes from getLAOffset
 *
 *  @param {Date} date to use for local date values
 *  @returns {Date} set to midnight in LA
 */
function getLAMidnight(date) {
  var d = new Date(+date);
  
  // Get offset. If hour is before offset, set to offset
  // If hour is after offset, set to offset tomorrow
  // Re-check offset and adjust if necessary
  var offset = getLAOffset(d);
  var midLA = d.setUTCHours(-offset, 0, 0, 0);
  if (d < date) d.setUTCDate(d.getUTCDate() + 1);
  d.setUTCHours(-getLAOffset(d));
  return d;
}

// Local date and time for midnight LA tomorrow:
[new Date(2018,0, 1),     //  1 Jan 2018
 new Date(2018,2,11),     // 11 Mar 2018
 new Date(2018,2,11, 12), // 11 Mar 2018, noon
 new Date(2018,2,12),     // 12 Mar 2018
 new Date(2018,5, 1),     //  1 Jun 2018
 new Date(2018,10,4),     //  4 Nov 2018
 new Date(2018,11,1),     //  1 Dec 2018
 new Date()               // Current
].forEach(function(date) {
  console.log('Local date       : ' + date.toString() +
            '\nNext LA midnight : ' + getLAMidnight(date).toString());
});

这篇关于js,获取其他时区的午夜当地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆