Skip to content Skip to sidebar Skip to footer

Js, Get The Local Time At Midnight For A Different Timezone

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 m

Solution 1:

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
 */functiongetLAOffset(date) {
  // Get DST start in UTCvar start = newDate(Date.UTC(date.getUTCFullYear(), 2, 1, 10));
  start.setUTCDate(start.getUTCDate() + (7-start.getUTCDay())%7 + 7);
  // Get DST end in UTCvar end = newDate(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
 */functiongetLAMidnight(date) {
  var d = newDate(+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 necessaryvar 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:
[newDate(2018,0, 1),     //  1 Jan 2018newDate(2018,2,11),     // 11 Mar 2018newDate(2018,2,11, 12), // 11 Mar 2018, noonnewDate(2018,2,12),     // 12 Mar 2018newDate(2018,5, 1),     //  1 Jun 2018newDate(2018,10,4),     //  4 Nov 2018newDate(2018,11,1),     //  1 Dec 2018newDate()               // Current
].forEach(function(date) {
  console.log('Local date       : ' + date.toString() +
            '\nNext LA midnight : ' + getLAMidnight(date).toString());
});

Solution 2:

I believe that the snippet in your question is quite close but it appears that you are adding the offset in the wrong direction.

You might encounter problems with daylight savings time - for instance, LA is at UTC-7h during the summer months but UTC-8h normally. If you don't want to use moment.js then the only option I can think of is to parse a locale-formatted string of the time.

This is not the most robust method and will only work (as is) for en-US formatted times. The America/Los_Angeles timezone might also not be available depending on the browser.

let now = newDate()

// Get the current time in LA using string matching to enable the offset to be calculated// This allows for daylight savings to be considered// Caution the LocaleTimeString is en-US formatted. Take care if using a different localelet timeInLA = now.toLocaleTimeString('en-US', { timeZone: 'America/Los_Angeles' })
  .match(/([0-1]?[0-9])\:([0-5]?[0-9])\:[0-5]?[0-9]\s(AM|PM)/)
  // trim first element of match
  .slice(1)
   // take account of AM/PM and convert values to integers
  .map( (e,i,a) => i==0 && a[3] =='PM' ? +e+12 : +e)
  // trim AM/PM
  .slice(0,-1)
  
// positive values mean local time is ahead of LAlet offsets = [now.getHours(),now.getMinutes()].map((e,i) => e-timeInLA[i])

// sum up hours and minutes to get totalOffset and convert to mslet totalOffsetMS = (offsets[0]*3600 + offsets[1]*60)*1000// local midnight Tomorrow - add 24 hours and zero other valueslet localMidnightTomorrow = newDate(+now.setHours(24,0,0,0))

// LA midnight will happen at the local midnight time with the offset addedlet localTimeWhenMidnightInLA = newDate(+localMidnightTomorrow + totalOffsetMS)

console.log(`It will be "${localTimeWhenMidnightInLA.toDateString()}${localTimeWhenMidnightInLA.toTimeString()}" in your location when it is midnight in LA tomorrow`)

Post a Comment for "Js, Get The Local Time At Midnight For A Different Timezone"