user12345
user12345

Reputation: 261

Javascript to get Current Date irrespective of Time Zone

I am looking for javascript code which will give me 1st date of the current month according to local timezone (client browser timezone).

Currently I am using the code below:

var startDate =new Date (2012,5,1);

Which correctly gives me this value:

Fri Jun 1 00:00:00 EDT 2012

But when I try the same thing in a browser in Beirut, which is +2.00 GMT, it gives May 31st "at times" (meaning if I try in the morning it gives me 31st but Beriut PM time works fine)

Basically I need to get a local browser current 1st date. Any suggestions?

Upvotes: 3

Views: 1188

Answers (1)

Martin Borthiry
Martin Borthiry

Reputation: 5326

Calling the Date function without parameters, always return the current time for the browser, which takes the time and timezone from the OS.

Try this:

var now = new Date(),
    first = new Date(now.getFullYear(), now.getMonth(), 1);
alert(first);

​ You can try to see the time zone using now.getTimezoneOffset();

Upvotes: 3

Related Questions