如何在javascript中获取日期的开始 - 在时区中进行因子分解

时间:2011-12-09 20:42:59

标签: javascript mongodb timezone

我正在努力找出javascript中时区的因素开始。请考虑以下事项:

   var raw_time = new Date(this.created_at);
   var offset_time = new Date(raw_hour.getTime() + time_zone_offset_in_ms);

   // This resets timezone to server timezone
   var offset_day = new Date(offset_time.setHours(0,0,0,0))
   // always returns 2011-12-08 05:00:00 UTC, no matter what the offset was!

   // This has the same issue:
   var another_approach_offset_day = new Date(offset_time.getFullYear(),offset_time.getMonth(),offset_time.getHours())

我希望当我通过太平洋时区偏移时,得到:2011-12-08 08:00:00 UTC等等。

实现这一目标的正确方法是什么?

我认为问题的一部分是setHours方法根据当地时间设置小时(从0到23)。

另请注意,我使用嵌入在mongo中的javascript,因此我无法使用任何其他库。

谢谢!


Jeez,所以这对我来说真的很难,但这是我提出以下解决方案的最终解决方案。诀窍是我需要使用setHours或SetUTCHours来获得一天的开始 - 我唯一的选择是系统时间和UTC。所以我得到UTC日的开始,然后加上偏移!

// Goal is given a time and a timezone, find the beginning of day
function(timestamp,selected_timezone_offset) {
  var raw_time = new Date(timestamp)
  var offset_time = new Date(raw_time.getTime() + selected_timezone_offset);
  offset_time.setUTCHours(0,0,0,0);
  var beginning_of_day = new Date(offset_time.getTime() - selected_timezone_offset);
  return beginning_of_day;
}

5 个答案:

答案 0 :(得分:5)

在JavaScript 所有日期存储为UTC。也就是说,date.valueOf()返回的序列号是自1970-01-01 00:00:00 UTC以来的毫秒数。但是,当您通过.toString().getHours()等查看日期时,您会获得当地时间的值。也就是说,系统运行脚本的本地时间。您可以使用.toUTCString().getUTCHours()等方法获取UTC值。

所以,你不能在任意时区获取日期,它都是UTC(或本地)。但是,当然,如果您知道UTC偏移,您可以在任何您喜欢的时区获得日期的字符串表示。最简单的方法是从日期中减去UTC偏移量,然后拨打.getUTCHours().toUTCString()或您需要的任何内容:

var d = new Date();
d.setMinutes(d.getMinutes() - 480); // get pacific standard time
d.toUTCString(); // returns "Fri, 9 Dec 2011 12:56:53 UTC"

当然,如果您使用.toUTCString(),则最后需要忽略“UTC”。你可以去:

d.toUTCString().replace(/UTC$/, "PST");

修改:时区重叠日期边界时不要担心。如果您传递setHours()一个负数,它将从昨天的午夜减去这些小时数。例如:

var d = new Date(2011, 11, 10, 15); // d represents Dec 10, 2011 at 3pm local time
d.setHours(-1);                     // d represents Dec 9, 2011 at 11pm local time
d.setHours(-24);                    // d represents Dec 8, 2011 at 12am local time
d.setHours(52);                     // d represents Dec 10, 2011 at 4am local time

答案 1 :(得分:0)

您使用的time_zone_offset_in_ms变量来自哪里?也许它不可靠,你应该使用Date的getTimezoneOffset()方法。以下网址有一个示例:

http://www.w3schools.com/jsref/jsref_getTimezoneOffset.asp

答案 2 :(得分:0)

如果您知道其他日期字符串中的日期,则可以执行以下操作:

var currentDate = new Date(this.$picker.data('date'));
var today = new Date();
today.setHours(0, -currentDate.getTimezoneOffset(), 0, 0);

(基于我所做项目的代码库)

答案 3 :(得分:0)

var aDate = new Date();
var startOfTheDay = new Date(aDate.getTime() - aDate.getTime() % 86400000)

将创建当天的开始,即当天的

答案 4 :(得分:0)

您可以使用Intl.DateTimeFormat。这也是luxon处理时区的方式。

下面的代码可以将任何时区的任何日期转换为时间的开始/结束时间。

const beginingOfDay = (options = {}) => {
  const { date = new Date(), timeZone } = options;
  const parts = Intl.DateTimeFormat("en-US", {
    timeZone,
    hourCycle: "h23",
    hour: "numeric",
    minute: "numeric",
    second: "numeric",
  }).formatToParts(date);
  const hour = parseInt(parts.find((i) => i.type === "hour").value);
  const minute = parseInt(parts.find((i) => i.type === "minute").value);
  const second = parseInt(parts.find((i) => i.type === "second").value);
  return new Date(
    1000 *
      Math.floor(
        (date - hour * 3600000 - minute * 60000 - second * 1000) / 1000
      )
  );
};

const endOfDay = (...args) =>
  new Date(beginingOfDay(...args).getTime() + 86399999);

const beginingOfYear = () => {};

console.log(beginingOfDay({ timeZone: "GMT" }));
console.log(endOfDay({ timeZone: "GMT" }));
console.log(beginingOfDay({ timeZone: "Asia/Tokyo" }));
console.log(endOfDay({ timeZone: "Asia/Tokyo" }));