beano
beano

Reputation: 952

Javascript - Determine date format based on timezone

I have two date strings and a timezone (which will vary for each user). With this information I need to construct two date objects.

//information I have
var date1 = '05/05/2018'
var date2 = '06/05/2018'
var timezone = 'Australia/Sydney'

//date objects
var date1 = new Date(date1); // Sat May 05 00:00:00 GMT+00:00 2018
var date2 = new Date(date2); // Tue Jun 05 00:00:00 GMT+00:00 2018

Problem

date2 should be 6th of May (rather than 5th of June).

Since I have the timezone, is there a javascript function that will allow me to pass the date along with the timezone and it to automatically determine the correct date format (e.g dd/mm or mm/dd)?

Upvotes: 0

Views: 41

Answers (1)

Vitaly Menchikovsky
Vitaly Menchikovsky

Reputation: 8884

I think the simple way to work with momentjs, You can find example like this:

var a = moment.tz("2013-11-18 11:55", "America/Toronto");
var b = moment.tz("May 12th 2014 8PM", "MMM Do YYYY hA", "America/Toronto");
var c = moment.tz(1403454068850, "America/Toronto");

Upvotes: 1

Related Questions