toDate with .utc is using local timezone - javascript

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?

Related

How could I make the moment.Moment to be in UTC, but preserve the date and time?

How could I make the moment.Moment to be in UTC, but preserve the date and time?
E.g. I have the moment.Moment which holds a non UTC time:
Tue Sep 14 2021 00:00:00 GMT+0200 (Central European Summer Time)
I want to have a moment.Moment which would have the same time, i.e. Tue Sep 14 2021 00:00:00, but in UTC.

Browser shows repeating datetime values after DST change

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

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 constructors provide unexpected results when called with similar arguments

I got one weird issue with Date object initialization. And wondering if someone can explain why..
var exp1 = new Date('2014-10-17');
var exp2 = new Date(2014,9,17);
var exp3 = new Date('17 Oct 2014');
console.log(exp1);
console.log(exp2);
console.log(exp3);
Results:
Thu Oct 16 2014 18:00:00 GMT-0600 (MDT) // 16th?
Fri Oct 17 2014 00:00:00 GMT-0700 (MST) // Why GMT -7
Fri Oct 17 2014 00:00:00 GMT-0600 (MDT) // The only one that works as expected
Why are these three Date objects so different?
The first date is treated as GMT since no time zone offset is provided. When logged out it shows the time in your local timezone. Adding an offset (exp4 below), I get the date expected.
var exp1 = new Date('2014-10-17');
var exp2 = new Date(2014,9,17);
var exp3 = new Date('17 Oct 2014');
var exp4 = new Date('2014-10-17z-0500');
Results:
Thu Oct 16 2014 19:00:00 GMT-0500 (Central Daylight Time)
Fri Oct 17 2014 00:00:00 GMT-0500 (Central Daylight Time)
Fri Oct 17 2014 00:00:00 GMT-0500 (Central Daylight Time)
Fri Oct 17 2014 00:00:00 GMT-0500 (Central Daylight Time)
I am not sure about exp2 for you, but suspect it has something to do with daylight savings time and that you live in an area that does not observe daylight savings (Arizona?).
Edit: this seems to be browser specific. The results above were generated in Chrome while in IE 11, exp4 was an invalid date. For IE 11 I had to use this format:
var exp4 = new Date('2014-10-17T00:00-05:00');

Strange date parsing issue

I'm loading some dates comming from my database into a HTML table in a string format. The string looks like 31-AUG-13 I'm parsing this string into a date object using the below code:
var paymentDate = $(this).find('td.paymentDate').text();
var test = $.datepicker.parseDate('d-M-y', paymentDate);
Everything is ok so far and I'm getting this date object: Date {Sat Aug 31 2013 00:00:00 GMT+0300 (FLE Standard Time)} But once the year is bigger than 2023. In my case 31-JAN-24 and so on it is turning to 1924 and not 2024, so I'm getting these date objects:
Date {Thu Jan 31 1924 00:00:00 GMT+0200 (FLE Daylight Time)}
Date {Fri Feb 29 1924 00:00:00 GMT+0200 (FLE Daylight Time)}
Date {Mon Mar 31 1924 00:00:00 GMT+0300 (FLE Standard Time)}
Date {Wed Apr 30 1924 00:00:00 GMT+0300 (FLE Standard Time)}
And so on. My question is regarding this strange issue. Is there a way to declare the year range and why it is going back to 1900 in the case when the year is bigger than 2023?
This teaches a lesson always use year in full format same was case with y2k problem. Convert date from database in to yyyy format then use it.

Categories

Resources