Browser shows repeating datetime values after DST change - javascript

I am trying to display some datatime values in the a data table, here is the example code
<html>
<head>
<script type="text/javascript">
console.log(new Date(2017,2,26,0,0,0));
console.log(new Date(2017,2,26,1,0,0));
console.log(new Date(2017,2,26,2,0,0));
console.log(new Date(2017,2,26,3,0,0));
</script>
</head>
<body>
</body>
</html>
the browser shows repeating values for 2 AM.
Here is the log output
Sun Mar 26 2017 00:00:00 GMT+0000 (GMT Standard Time)
Sun Mar 26 2017 02:00:00 GMT+0100 (GMT Daylight Time)
Sun Mar 26 2017 02:00:00 GMT+0100 (GMT Daylight Time) **(Should it not be 3am)**
Sun Mar 26 2017 03:00:00 GMT+0100 (GMT Daylight Time) **(Should it not be 4am)**
I know I can use Date.UTC()
console.log(new Date(Date.UTC(2017,2,26,0,0,0)));
console.log(new Date(Date.UTC(2017,2,26,1,0,0)));
console.log(new Date(Date.UTC(2017,2,26,2,0,0)));
console.log(new Date(Date.UTC(2017,2,26,3,0,0)));
But that will display wrong date time values in other time zones.
Can someone suggest a solution to this problem? Or can someone explain what is happening here...?

This behavior depends on where time zone that is currently set on your computer is participating in daylight savings time Does Everyone Observe Daylight Saving Time?
If you switch you locale to Iceland time zone, for instance, (UTC+00:00) Monrovia, Rejkjavik and execute the code in console:
new Date(2017,2,26,0,0,0)
new Date(2017,2,26,1,0,0)
new Date(2017,2,26,2,0,0)
new Date(2017,2,26,3,0,0)
You will see the output where all hours are observed:
Sun Mar 26 2017 00:00:00 GMT+0000 (Greenwich Standard Time)
Sun Mar 26 2017 01:00:00 GMT+0000 (Greenwich Standard Time)
Sun Mar 26 2017 02:00:00 GMT+0000 (Greenwich Standard Time)
Sun Mar 26 2017 03:00:00 GMT+0000 (Greenwich Standard Time)
For countries that do participate in daylight savings one our will be missing depending on their timezone.
You could also check (UTC+10:00) Brisbane, Australia you see that all hours from midnight of 26th to midnight of 27th of March are displayed correctly.

If you can use libraries I highly recomend you to use MomentJS, it will make your life easier.
console.log(moment("2017-02-26 00:00:00").format());
console.log(moment("2017-02-26 01:00:00").format());
console.log(moment("2017-02-26 02:00:00").format());
console.log(moment("2017-02-26 03:00:00").format());

Related

toDate with .utc is using local timezone

https://momentjs.com/docs/#/parsing/utc/
moment.utc(ts).format(); // 2018-10-25T05:00:00+00:00
moment.utc(ts).toDate(); // Thu Oct 25 2018 07:00:00 GMT+0200 (Central European Summer Time)
How do I get the toDate format of the date but lose the local timezone (so that the second line displays 05:00:00 GMT+0000 (Central European Summer Time)). I thought using ".utc" fixed this?

Javascript Date() timezone incosistency

I get inconsistent timezone based on params to Date():
new Date()
Sun Oct 25 2015 18:10:42 GMT+0200 (IST)
new Date(1445720400)
Sat Jan 17 1970 19:35:20 GMT+0200 (IST)
new Date(144572040000)
Thu Aug 01 1974 09:54:00 GMT+0300 (IDT)
new Date(14457204000000)
Thu Feb 17 2428 20:00:00 GMT+0200 (IST)
I tried reading the docs or finding an explanation to this weirdness, but couldn't.
I've checked on both Chrome 46 and Safari 7.1.8,
Any ideas?
Isn't this just daylight savings? One of the dates happened to be in the summer?
The problem in then you set different time in ms as param for 'new Date()'. And you have different time zones because the Date has been generated in different seasons (Summer's time and Winter's time). It is normal.

check if a date falls in the last week using date.js

i use date.js for doing certain date calculations.
i am able to find if the date falls in this week, the following returns true
dateFld.between(Date.monday(), Date.friday())
but I want to check if date falls in the previous week.
i am using the following code without luck.
alert(dateFld.between(Date.last().week().monday(), Date.last().week().sunday()));
please help.
Sunday is the first day of the week.
Date.last().week().monday()
Mon Sep 07 2015 00:00:00 GMT+0100 (GMT Daylight Time) Correct
Date.last().week().sunday()
Sun Sep 06 2015 00:00:00 GMT+0100 (GMT Daylight Time) Incorrect
Date.last().sunday()
Sun Sep 13 2015 00:00:00 GMT+0100 (GMT Daylight Time) Correct

Date returning inconsistent results (depending whether leading zero exists)

> new Date('2015-1-1')
Thu Jan 01 2015 00:00:00 GMT-0500 (EST)
> new Date('2015-01-1')
Thu Jan 01 2015 00:00:00 GMT-0500 (EST)
> new Date('2015-1-01')
Thu Jan 01 2015 00:00:00 GMT-0500 (EST)
// Yet...
> new Date('2015-01-01')
Wed Dec 31 2014 19:00:00 GMT-0500 (EST)
// Similarly:
> new Date('2015-1-10')
Sat Jan 10 2015 00:00:00 GMT-0500 (EST)
> new Date('2015-01-10')
Fri Jan 09 2015 19:00:00 GMT-0500 (EST)
Can't figure out why this is happening (Chrome 39). Is it related to octal parsing?
Firefox only accepts new Date('2015-01-10'), and returns what I expect: Date 2015-01-10T00:00:00.000Z
Found the answer in a related question; it appears Chrome parses the YYYY-MM-DD format as UTC time, then converts it the local timezone. So, 2015-01-01 00:00:00 in UTC is Dec 31 in EST.
See Inconsistencies when creating new date objects:
It looks like the form '1979-04-05' is interpreted as a UTC date (and then that UTC date is converted to local time when displayed).
Apparently, a possible cross browser solution is to replace the dashes with slashes to force using local time:
new Date('2015-01-10'.replace(/-/g, '/'))
I am unsure of your problem since My chrome(39.0.2171.99) gives me Jan 01 in all case. But having said this, I would like to point out that you should probably use
new Date(2015,1,1)
This is how JS Date is supposed to be initialised.

Parse String Dates with different timezones

I am trying to be able to take many strings with many different formats and in different timezones and turn them into either UTC or my localtime. I have tried the following and for some reason it has given me a hour off:
var moment = require('moment');
console.log(moment('Mon, 30 Sep 2013 18:00:00 EST').format()); //2013-09-30T16:00:00-07:00
console.log(new Date('Mon, 30 Sep 2013 18:00:00 EST')); //Mon Sep 30 2013 16:00:00 GMT-0700 (PDT)
console.log(new Date()); //Mon Sep 30 2013 15:00:00 GMT-0700 (PDT)
The only thing I can think of that could cause this is day light savings time but I am not sure. Any suggestions with how to proceed?
You used the wrong time zone. For an apples-to-apples comparison, use EDT (eastern daylight time):
> console.log(new Date('Mon, 30 Sep 2013 18:00:00 EDT'));
Mon Sep 30 2013 15:00:00 GMT-0700 (PDT)
which is what you would expect (3 hour difference)

Categories

Resources