Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new Date() for a specific timezone in JavaScript

I am working on a project and I was just curious if it was possible in JavaScript to call new Date() for a specific timezone. When I say:

var test = new Date();

I debug and I find that the var test =

Date {Fri Nov 07 2014 11:59:41 GMT-0500 (Eastern Standard Time)}

which makes sense because I am in the Eastern Time Zone and it is 11:59 here.

My questions is: Is there a way to get the Date object for a specified time zone, and not just your own local system time?

I have a reservation form where I am comparing the time entered for the start of a conference with the actual time is it now to make sure that the user doesn't enter a past time, or a time within 2 hours from the current time(different error validation messages get thrown up for each instance).

My only problem is that the person who fills out the form can be either in Eastern, Central, Mountain, or Pacific. So if a person located in Eastern Time fills out a form for a conference with a start time for a customer in Mountain time, when I try to compare the times, new Date() only gives me my local time, when I would want it to compare the time entered to Mountain time for my checks and validation.

Its sort of confusing but I just wanted to give you a little background on the application for this issue. If anyone needs further clarification please feel free to ask. Any help is greatly appreciated.

Thank you!

-Dave

like image 421
MotoDave452 Avatar asked Aug 13 '26 01:08

MotoDave452


1 Answers

Checkout moment-timezone.js which makes working with Dates and timezones a lot easier in javascript.

The example on the frontpage shows how it works:

var newYork    = moment.tz("2014-06-01 12:00", "America/New_York");
var losAngeles = newYork.clone().tz("America/Los_Angeles");
var london     = newYork.clone().tz("Europe/London");

newYork.format();    // 2014-06-01T12:00:00-04:00
losAngeles.format(); // 2014-06-01T09:00:00-07:00
london.format();     // 2014-06-01T17:00:00+01:00
like image 168
Rovak Avatar answered Aug 15 '26 16:08

Rovak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!