Firefox Date String - javascript

I'm having trouble with firefox converting date object to date string.
Here is the piece of my JS code:
var dt = Date('2014-05-10');
alert(dt.toString());
My timezone on my PC is set to Eastern Time Zone.
This is what I am getting using different browsers:
Firefox:
Wed May 14 2014 10:13:52 GMT-0400 (Eastern Standard Time)
Chrome:
Wed May 14 2014 10:14:30 GMT-0400 (Eastern Daylight Time)
IE:
Wed May 14 10:15:14 EDT 2014
Any idea why I am getting EST on Firefox?

Related

Time value get increased based on offset in safari browser in JS

While parsing the ISO string with new Date() method in safari browser, it will calculated time value based on the client timezone offset. For ex. If we provide the ISO string as "2019-10-17T00:00:00", then it will produces the output "Thu Oct 17 2019 05:30:00 GMT+0530 (IST)". But, chrome displays the correct value like "Thu Oct 17 2019 00:00:00 GMT+0530". Is there any issues in the safari browser and how to overcome this issue?
In safari browser: new Date("2019-10-17T00:00:00") => Thu Oct 17 2019 05:30:00 GMT+0530 (IST)
In Chrome browser: new Date("2019-10-17T00:00:00") => Thu Oct 17 2019 00:00:00 GMT+0530

Date string conversion browser differences with daylight saving time [duplicate]

This question already has answers here:
Browsers, time zones, Chrome 67 Error (historic timezone changes)
(2 answers)
Closed 4 years ago.
I have 2 questions.
1. Regarding daylight saving time implementation in JavaScript in browsers.
( Output is from Chrome Browser)
var dt1 = new Date(1975, 1 , 23, 1, 0, 0);
dt1.toString(); // Sun Feb 23 1975 01:00:00 GMT-0600 (Central Standard Time)
dt1.toUTCString(); // Sun, 23 Feb 1975 07:00:00 GMT
dt1.getTime(); // 162370800000
var dt2 = new Date(1975, 1 , 23, 2, 0, 0);
dt2.toString(); // Sun Feb 23 1975 03:00:00 GMT-0500 (Central Daylight Time)
dt2.toUTCString(); // Sun, 23 Feb 1975 08:00:00 GMT
dt2.getTime(); //162374400000
var dt3 = new Date(1975, 1 , 23, 3, 0, 0);
dt3.toString(); // Sun Feb 23 1975 03:00:00 GMT-0500 (Central Daylight Time)
dt3.toUTCString(); // Sun, 23 Feb 1975 08:00:00 GMT
dt3.getTime(); // 162374400000
For the date objects dt2 and dt3, observe all the values are same. This is confusing.
2. Internet explorer has wrong daylight saving time start dates.
In year 1975, daylight saving time stared on date Feb 23 1975 2:00 AM.
When I create date objects for dates Feb 23 1975 1AM, 2 AM and Apr 6 1975 1AM, 2AM, 3AM in Chrome and IE...
Feb 23 1975 1AM
Chrome: Sun Feb 23 1975 01:00:00 GMT-0600 (Central Standard Time)
IE : Sun Feb 23 1975 01:00:00 GMT-0600 (Central Standard Time)
Feb 23 1975 2AM
Chrome: Sun Feb 23 1975 03:00:00 GMT-0500 (Central Daylight Time)
IE : Sun Feb 23 1975 02:00:00 GMT-0600 (Central Standard Time)
Apr 6 1975 1AM
Chrome: Sun Apr 06 1975 01:00:00 GMT-0500 (CDT) //165996000000
IE : Sun Apr 06 1975 01:00:00 GMT-0600 (CST) //165999600000. Time in milliseconds is also different.
Apr 6 1975 2AM
Chrome: Sun Apr 06 1975 02:00:00 GMT-0500 (CDT) // 165999600000
IE : Sun Apr 06 1975 01:00:00 GMT-0600 (CST) // 165999600000. Time in milliseconds is same.
Apr 6 1975 3AM
Chrome: Sun Apr 06 1975 03:00:00 GMT-0500 (CDT)
IE : Sun Apr 06 1975 03:00:00 GMT-0500 (CDT)
In IE for year 1975, daylight saving time logic starts after Apr 6 1975 2PM and not after Feb 23 1975 2AM as it is supposed to be. Chrome is working properly.
How do I show proper date string in all browsers when daylight saving time is involved?
Daylight savings and timezones are the worst! :)
JavaScript Date/Time is always subject to the idiosyncrasies of each and every browser as it runs client side. So even 2 users with identical browsers may get variations based on their laptop settings.
To specifically answer the 2 questions in your post:
1: Looks like you are accessing the JavaScript from the Central Timezone which observes DST whereas UTC doesn't. Daylight savings time occurs at 2am, so dt1 will be in standard time as it is 1am on that that evening, where as dt2 & dt3 are in dst as they are 2am and 3am respectively. That's the reason for the difference.
2: Because Richard Nixon infamously mandated year-round daylight saving in 1974 and 1975. Appears the version of IE you are testing with handles the DST based on the executive mandate at the time, whereas Chrome applies it in a consistent format based on current guidelines.
Fortunately there is MomentJS and MomentTimezone which handles all these variations: Is moment.js browser independent?
These are essential JavaScript libraries for anything related to Date/Time
https://momentjs.com/
https://momentjs.com/timezone/
Easiest way to get going is:
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"> </script>
<script>
var now = moment();
console.log(now.format());
console.log(now.isDST());
</script>
You can place that inside your bodytag and look in the console and it'll be working for you. Have fun!

Datepicker, same code, different localization chrome/IE

I am using datepicker with localization.
It all works great but i have a problem.
var selected_date = jQuery('.datepicker').datepicker('getDate');
selected_date = new Date(selected_date);
Output on Chrome :
Wed Oct 24 2018 00:00:00 GMT+0300 (Eastern European Summer Time)
Output on IE 11 :
Wed Oct 24 2018 00:00:00 GMT+0300 (GTB Summer Time)
How is that possible happening in same server, same time and same conditions.

Javascript huge date bug in windows... Solutions?

My dev environment is in mac.
I use bootstrap datepicker to choose dates in an application.
When a day like 27/11/2013 is selected in mac when I debug, the screen shows...
day: 1385510400
and the translation to an date object in the same debug tool is,
Wed Nov 27 2013 00:00:00 GMT+0000 (GMT)
which is correct.
All is OK, since here.
I upload the code to a windows environment and open the same page with IE8.
Start the nightmare... :)
In windows, the same day variable is shown like that...
day: 1385506800
and the translation to an date object in the same debug tool is,
Wed Nov 27 2013 00:00:00 GMT+0000 (GMT)
which, is not correct?
If we go to a external tool, the day that gave IE8 is day before, and the translation should be,
Tue, 26 Nov 2013 23:00:00 GMT.
How I can understand this?
There is no relation with the timestamp and the translation to date object, and obviously I can't find a way to make this works in both systems.
Could you wake me up from this nightmare?
Sorry for my english!
Fiddle
In JS in Chrome:
new Date(1385510400000) : Wed Nov 27 2013 01:00:00 GMT+0100
new Date(1385506800000) : Wed Nov 27 2013 00:00:00 GMT+0100
new Date("11/27/2013") : Wed Nov 27 2013 00:00:00 GMT+0100
Please note the order of the date string, it is in US order
Also note the 000 at the end; JS timestamps are in milliseconds
The same in IE8
Wed Nov 27 01:00:00 UTC+0100 2013
Wed Nov 27 00:00:00 UTC+0100 2013
Wed Nov 27 00:00:00 UTC+0100 2013
So exactly the same dates, on hour difference on the first
If you subtract the 1 hour from all dates, you get midnight on the first and 11pm on the 26th on the rest
To normalise you can do
var d = new Date(1385510400000);
d.setHours(0,0,0);
or change to UTC

How does JavaScript's Date work?

Can someone explain me the output http://jsfiddle.net/mark69_fnd/NhuLe/ produces?
new Date('2012-07-01') == Sat Jun 30 2012 20:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2012-07-09') == Sun Jul 08 2012 20:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2012-07-10') == Mon Jul 09 2012 20:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2012-07-31') == Mon Jul 30 2012 20:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2012-08-1') == Wed Aug 01 2012 00:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2012-08-9') == Thu Aug 09 2012 00:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2012-08-10') == Thu Aug 09 2012 20:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2012-08-31') == Thu Aug 30 2012 20:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2012-09-1') == Sat Sep 01 2012 00:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2012-09-9') == Sun Sep 09 2012 00:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2012-09-10') == Sun Sep 09 2012 20:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2012-12-09') == Sat Dec 08 2012 19:00:00 GMT-0500 (Eastern Standard Time)
new Date('2012-12-31') == Sun Dec 30 2012 19:00:00 GMT-0500 (Eastern Standard Time)
new Date('2013-01-01') == Mon Dec 31 2012 19:00:00 GMT-0500 (Eastern Standard Time)
new Date('2013-01-09') == Tue Jan 08 2013 19:00:00 GMT-0500 (Eastern Standard Time)
new Date('2013-02-09') == Fri Feb 08 2013 19:00:00 GMT-0500 (Eastern Standard Time)
new Date('2013-03-09') == Fri Mar 08 2013 19:00:00 GMT-0500 (Eastern Standard Time)
new Date('2013-04-09') == Mon Apr 08 2013 20:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2013-05-09') == Wed May 08 2013 20:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2013-06-09') == Sat Jun 08 2013 20:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2013-07-09') == Mon Jul 08 2013 20:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2013-08-09') == Thu Aug 08 2013 20:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2013-09-09') == Sun Sep 08 2013 20:00:00 GMT-0400 (Eastern Daylight Time)
I am interested to understand how it decides to compute the day. Please, note the difference between 2012-07-09, 2012-08-9 and 2013-08-09.
I ran it on Chrome.
This is a very interesting, and subtle, question.
The reason is that some of your dates are in the ISO-8601-like format defined in the specification, and so are parsed as GMT, but others are not, and so they fall back on non-standard date parsing, which appears to be (in Chrome) using local time instead.
The date string 2012-07-01 conforms to the format specified in Section 15.9.1.15, and so according to the rules of that section, it is parsed in timezone Z (GMT). Then you output it and it's output in local time, four hours or so earlier, and so the date changes as the original value (having no time part) is at midnight.
The date string 2012-08-1 does not conform to that format (it needs a 0 before the 1). This takes us out of the land of specified behavior. The Date constructor, when given a string, follows the same rules as Date.parse, which are defined in Section 15.9.4.2, which says amongst other things:
The function first attempts to parse the format of the String according to the rules called out in Date Time String Format (15.9.1.15). If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats.
(My emphasis)
The moral of this story is: Stick to specified formats. :-)
But a side note on that: The date/time format defined in the spec is relatively new (as of ES5, about three years ago). Prior to that, there was no defined date/time format that the Date constructor (or Date parse) was required to parse. It just had to be able to parse whatever Date#toString spat out, but what that was was implementation-specific. And older browsers will indeed fail to parse 2012-08-01. Although it's not specified, nearly all browsers (every one I've ever tested) will parse 2012/08/01, though. Of course, now I want to go back and see what time zone they use (and check whether they all use the same one)...
new Date('2012-07-01') means that you are providing date in *GMT 000*0 timezone.
But when it displays you the date it does the same in your browser timezone(which is GMT -400 in your case).
Thats is the reason that you see all the timings to be 20:00.

Categories