Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 873
user12345 Avatar asked Dec 27 '25 21:12

user12345


1 Answers

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();

like image 191
Martin Borthiry Avatar answered Dec 30 '25 10:12

Martin Borthiry



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!