How to get the start time and end time in utc of a day for a specified timezone in javascript?
Asked Answered
B

3

20

As the title stated, I want to get the start time and end time of a day for certain timezone and convert them to utc time. Here is my part of my implementation:

//convert current local time to specified timezone time
var converted = moment.tz(moment(), timezone).format("YYYY-MM-DD");
var full_format = "YYYY-MM-DD HH:mm:ss";

// get start time and end time in the timezone
var start = converted + " 00:00:00";
var end = converted + " 23:59:59";

// how to convert them in utc time by momentjs or other methods? 
var utc_start_time = ?
var utc_end_time = ?

The question is how to convert the time in certain timezone to utc time. Or is there any other decent solutions to it? Thank you!

Edit:

I figured out a way to make it myself which is not quite decent.

var converted = moment.tz(moment(), timezone).format("YYYY-MM-DD");         
var full_format = "YYYY-MM-DD HH:mm:ss";
var start = converted + " 00:00:00";
var end = converted + " 23:59:59";
var utc_start_time = moment(start).add(utc_offset * -1, 'hours').format(full_format);
var utc_end_time = moment(end).add(utc_offset * -1, 'hours').format(full_format); 

Any suggestions on improvements are welcome. Thanks

Bassarisk answered 16/3, 2016 at 9:15 Comment(6)
Possible duplicate of How do you convert a JavaScript date to UTC?Cowley
are you trying to calculate the gap between them?Malonis
The questions is not quite identical to [How do you convert a JavaScript date to UTC? ](#949032). The time to process does not contain any timezone info in it.Bassarisk
@Malonis Nope, just trying to get the utc time of the converted time.Bassarisk
is this helping?Malonis
@Zamboney, Matt gave the answer I want. But thank you anyway.Bassarisk
R
32

Depending on what exactly you want to do:

// for the current day
var start = moment.tz(timezone).startOf('day').utc();
var end = moment.tz(timezone).endOf('day').utc();

// for a specific moment (m)
var start = m.clone().tz(timezone).startOf('day').utc();
var end = m.clone().tz(timezone).endOf('day').utc();

// for a specific calendar date
var m = moment.tz(date, format, timezone);
var start = m.clone().startOf('day').utc();
var end = m.clone().endOf('day').utc();

You can then format start and end however you like with the format function.

Keep in mind also that not every day starts at midnight in every time zone, nor do all local days have 24 hours.

Recurrent answered 16/3, 2016 at 16:22 Comment(2)
thanks for the reply again. It did answer my question. I should have read thru the doc before asking. But would you give me an example on in which situation a day does not start at 12am or not have 24 hours.Bassarisk
Any day with a time zone transition, particularly those related to daylight saving time may have more or less than 24 hours by the local clock. Time zones that have a forward-moving transition right at midnight will skip over midnight. Brazil is a good example. Consider that on October 16, 2016, the day will start at 1:00 in São Paulo, BrazilRecurrent
T
4

Unfortunately, moment.js seems to be telling people to use other options. Also, many of the moment functions in the accepted answer are now deprecated. Luxon is the first library that moment suggest as an alternative.

let DateTime = luxon.DateTime;

var dt = DateTime.local() // get the current time in local timezone
  .startOf('day')         // set the time to the start of the current day
  .setZone('GMT');        // change time zone back to GMT (zero offset)
console.log('Start: ' + dt.toISO());  // UTC equivalent for local start of day

var dt = DateTime.local() // get the current time in local timezone
  .endOf('day')           // set the time to the end of the current day
  .setZone('GMT');        // change time zone back to GMT (zero offset)
console.log('End  : ' + dt.toISO());  // UTC equivalent for local end of day (1 ms before midnight)
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/global/luxon.min.js"></script>
Taxexempt answered 12/1, 2021 at 5:50 Comment(1)
None of the methods in Matt's answer are currently deprecated. However, according to the project status page, Moment.js as a library is "done" and is in maintenance mode. Given the Temporal proposal will supplement (and in practice all but replace) the built–in Date, I suspect that Date will not change for a very, very long time (it hasn't changed significantly in the last 25 years) so moment.js will continue to work for a similar period. :-)Contemptible
R
1

Here is a native solution that uses Intl.DateTimeFormat to handle timezones.

It can find the begining/end of the day with specified timezone.

To convert a Date instance to UTC time, use Date.prototype.toISOString.

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" }));
Romeliaromelle answered 7/10, 2020 at 17:45 Comment(2)
In places where daylight saving is observed, this answer will be wrong on the DST changeover days for any time after the changeover time as it applies the wrong offset. The start of the day before the change has a different offset to the rest of the day after the change.Contemptible
Good solution for people who don't want to use moment or any other library.. please add DST logic as and its a perfect answer.Capacitance

© 2022 - 2024 — McMap. All rights reserved.