53127

Momentjs timezone - getting date at time in specific timezone

Question:

I am attempting to create a date from a UTC base in a user's specific timezone, in this case "America/Los Angeles" using momentjs/momentjs timezone. However, I'm not getting the results I expect:

var tempDate = moment(1448841600000); //moment("2015-11-30"); //monday 11/30 in UTC var adjustedStart = moment.tz(tempDate, "America/Los_Angeles"); adjustedStart.hour(9); adjustedStart.minute(30); console.log("adjustedStart in milliseconds:" + adjustedStart.valueOf());

The console output is 1448875800000 which is 11/30/15 9:30AM in UTC/GMT, I was expecting 1448904600000 which is 11/30/15 9:30AM in Pacific Coast time. How can I translate this start date to the right time in the user's timezone?

Answer1:

Yes, 1448841600000 is the date you said:

moment(1448841600000).utc().format() // "2015-11-30T00:00:00+00:00"

But that is a day earlier in Pacific time

moment(1448841600000).tz('America/Los_Angeles').format() // "2015-11-29T16:00:00-08:00"

When you adjust it to 9:30 pacific, it's on the 29th, not the 30th.

moment(1448841600000).tz('America/Los_Angeles').hour(9).minute(30).format() // "2015-11-29T09:30:00-08:00"

When you call valueOf, the result is:

moment(1448841600000).tz('America/Los_Angeles').hour(9).minute(30).valueOf() // 1448818200000

This is the correct value, however it's different than the one you provided. However, it is indeed what I get when I run your code as well.

Screenshot from Chrome debug window, with your exact code:

<a href="https://i.stack.imgur.com/flIVa.png" rel="nofollow"><img alt="screenshot" class="b-lazy" data-src="https://i.stack.imgur.com/flIVa.png" data-original="https://i.stack.imgur.com/flIVa.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Also, in comments you wrote:

//moment("2015-11-30"); //monday 11/30 in UTC

Actually, that would be in <em>local time</em>, not UTC. If you wanted UTC, you'd use:

moment.utc("2015-11-30")

Though it's unclear to me whether you are using this string input or the numeric timestamp.

If what you are asking is that you want the UTC date to be treated as if it were local and then have an arbitrary local time applied - that is a somewhat strange operation, but it would go something like this:

var tempDate = moment.utc(1448841600000); var adjustedStart = moment.tz([tempDate.year(), tempDate.month(), tempDate.date(), 9, 30], "America/Los_Angeles"); console.log("adjustedStart in milliseconds:" + adjustedStart.valueOf()); // adjustedStart in milliseconds:1448904600000

This gives the value you asked for, but to me - this is a smell that something is wrong with the expectation. I'd look a lot closer at the requirement and the other parts of the system.

Answer2:

From <a href="http://momentjs.com/docs/" rel="nofollow">http://momentjs.com/docs/</a>:

moment#valueOf simply outputs the number of milliseconds since the Unix Epoch

It is important to note that though the displays differ above, they are both the same moment in time.

var a = moment(); var b = moment.utc(); a.format(); // 2013-02-04T10:35:24-08:00 b.format(); // 2013-02-04T18:35:24+00:00 a.valueOf(); // 1360002924000 b.valueOf(); // 1360002924000

So it shouldn't differ for different timezones.

You should use adjustedStart.format(); to see the difference

Recommend

  • Convert Html to pdf with images
  • Cannot deserialize the current JSON array (e.g. [1,2,3]). C#, cant figure the error out
  • Can't separate cells properly with simplehtmldom
  • The content of my dropdown menu is closing too fast
  • android flash turn on/off using button while camera is running
  • How do I convert dates in this format to a date class in R?
  • getting the current time at specific timezone
  • Bing Virtual Earth 7.0 calculate area
  • Consistent Client Side Date/timestamp using JavaScript(considering TimeZones)
  • Gracefull shutdown for Spring boot Application
  • Get both date and time in milliseconds
  • JFreeChart heap space
  • toInstant() in Calendar is showing in GMT instead of Local time
  • Excel VBA Intersect
  • Convert unix time to week day
  • How to get the index of element in the List in c#
  • d3.js: why is d3.geo.path() giving NaN?
  • how to set to NULL all the filestream varbinary(max) fields?
  • countdown bar android example
  • Request response issues in biztalk
  • uml Composition relationships to RDF and OWL
  • D3 get axis values on zoom event
  • Detect when Facebook like button is clicked
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Upload files with Ajax and Jquery
  • Large data - storage and query
  • How to pass list parameters for each object using Spring MVC?
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • AngularJs get employee from factory
  • Proper way to use connect-multiparty with express.js?
  • python regex in pyparsing
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • need help with bizarre java.net.HttpURLConnection behavior
  • Android Google Maps API OnLocationChanged only called once
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • How can I use threading to 'tick' a timer to be accessed by other threads?