I've one script where I create two Date instance, the first return the date in CET offset timestamp and the second in CEST. I can't understand why.
var start_date = new Date(2014, 1, 26, 12, 0 , 0, 0);
var first_start_date = new Date(2014, 3, 1, 12, 0 , 0, 0);
return: Wed Feb 26 2014 12:00:00 GMT+0100 (CET) Tue Apr 01 2014 12:00:00 GMT+0200 (CEST)
I've also create a jsfiddle example
This is due to DST.
Also, see that CEST is Central European Summer Time.
It's just because April 1 is in Daylight Savings time (starts March 9th in the US this year), and February 26th is not in Daylight Savings time.
Related
This one has stumped me. It should be so simple I would think. I am doing some very simple date subtraction in Javascript. I am subtracting the same dates and I would think it would give zero hours, but it gives 18 hours.
let inDate = new Date('Tue Aug 27 2019 00:00:00 GMT-0500 (Central Daylight Time)').getTime();
let outDate = new Date('Tue Aug 27 2019 00:00:00 GMT-0500 (Central Daylight Time)').getTime();
document.getElementById('date').innerHTML = new Date(outDate - inDate);
<div id='date'>
</div>
In case it produces different results based on where you are, the result I am getting is this:
Wed Dec 31 1969 18:00:00 GMT-0600 (Central Standard Time)
This is due to your timezone. If you convert to GMT String before print it the time will be correct. (Jan 01, 1969 00:00:00)
new Date(outDate - inDate).toGMTString()
You should see the correct date.
let inDate = new Date('Tue Aug 27 2019 00:00:00 GMT-0500 (Central Daylight Time)').getTime()
let outDate = new Date('Tue Aug 27 2019 00:00:00 GMT-0500 (Central Daylight Time)').getTime()
console.log(new Date(inDate - outDate).toGMTString())
I am using node.js for a project and I am trying to add certain dates to an array. However, when I do, it overwrites the entire array with the current date being added. Source below:
let startDate = new Date(2014, 0, 8, 19, 0, 0)
let endDate = new Date(2014, 0, 11, 19, 0, 0)
let datesToDownload = []
let datesInDB = [new Date(2014, 0, 8, 19, 0, 0), new Date(2014, 0, 9, 19, 0, 0), new Date(2014, 0, 10, 19, 0, 0), new Date(2014, 0, 8, 11, 0, 0)]
for (let i = startDate; i <= endDate; i.setDate(i.getDate() + 1)) {
console.log('CHECKING DATE: ' + i.toDateString())
if (!(datesInDB2.indexOf(i.toDateString()) >= 0)) {
console.log('NEW RECORD FOUND FOR: ' + i.toDateString())
datesToDownload.push(i)
console.log('i: ' + i)
for (let j in datesToDownload) {
console.log('element ' + j + ': ' + datesToDownload[j])
}
}
}
And the output looks like this:
CHECKING DATE: Wed Jan 08 2014
NEW RECORD FOUND FOR: Wed Jan 03 2014
i: Wed Jan 08 2016 20:00:00 GMT-0400 (Eastern Daylight Time)
element 0: Wed Jan 08 2016 20:00:00 GMT-0400 (Eastern Daylight Time)
CHECKING DATE: Thur Jan 09 2016
NEW RECORD FOUND FOR: Thur Jan 09 2016
i: Thur Jan 09 2016 20:00:00 GMT-0400 (Eastern Daylight Time)
element 0: Thur Jan 09 2016 20:00:00 GMT-0400 (Eastern Daylight Time)
element 1: Thur Jan 09 2016 20:00:00 GMT-0400 (Eastern Daylight Time)
CHECKING DATE: Fri Jan 10 2016
NEW RECORD FOUND FOR: Fri Jan 10 2016
i: Fri Jan 10 2016 20:00:00 GMT-0400 (Eastern Daylight Time)
element 0: Fri Jan 10 2016 20:00:00 GMT-0400 (Eastern Daylight Time)
element 1: Fri Jan 10 2016 20:00:00 GMT-0400 (Eastern Daylight Time)
element 2: Fri Jan 10 2016 20:00:00 GMT-0400 (Eastern Daylight Time)
......
Notice how the elements are all changed to the current date being inserted, though an element is being added.
Why is this happening? How can I prevent it?
You're pushing exactly the same Date instance into the array on each iteration. You can create a copy of it however:
datesToDownload.push(new Date(i));
A date is an object, and is passed by reference. In your for loop you are changing the date:
for (let i = startDate; i <= endDate; i.setDate(i.getDate() + 1)) {
Why does
new Date(2016, 3, 30);
produce:
Sat Apr 30 2016 00:00:00 GMT+0100 (GMT Summer Time)
should it not be Wed Mar 30 2016 00:00:00 GMT+0100 (GMT Summer Time)
It is because
new Date(2016, 3, 30);
means
year: 2016
month: 3 - 0: jan, 1: feb, 2:mar, 3: april
day: 30
Just do this:
new Date(2016, 2, 30);
This code should log all days for given month:
var date = new Date(2012,2,1);
var thisMonth = date.getMonth();
while(date.getMonth()==thisMonth) { // 31 steps ???
console.log(date.getMonth(),date.getDate());
date.setDate(date.getDate()+1);
}
It works well for every month but February. Any ideas where is the catch?
Note the month parameter is 0-indexed, so your code is about March not February.
The doc:
month
Integer value representing the month, beginning with 0 for January to
11 for December.
Use new Date(2012,1,1); month is zero-based ;-)
This is pretty interesting:
new Date('2014-02-28'); // Fri Feb 28 2014 01:00:00 GMT+0100
new Date('2014-02-29'); // Sat Mar 01 2014 01:00:00 GMT+0100
new Date('2014-02-30'); // Sun Mar 02 2014 01:00:00 GMT+0100
new Date('2014-02-31'); // Mon Mar 03 2014 01:00:00 GMT+0100
new Date('2014-02-32'); // Invalid Date
While trying to find out why I was having issues while working on a calendar, I ran into this issue. When setting the month to 8, the date is set to October, when the month is set to 9, the date is set to October.
Code to test
var d = new Date();
document.write(d.getMonth());
d.setMonth(8);
document.write(d.getMonth());
d.setMonth(9);
document.write(d.getMonth());
output:
799
The current date is August 31st 2012, the month number should be 7, since the javascript months are 0 based.
Can someone explain this? I have been able to reproduce it on more than one computer.
September only has 30 days - when you set the day to 31 (or create a date on the 31st of some month) and then change the month to one with fewer than 31 days JavaScript rolls the date over into the next month (in this case October). In other words, the date overflows.
> var d = new Date()
> d
Fri Aug 31 2012 22:53:50 GMT-0400 (EDT)
// Set the month to September, leaving the day set to the 31st
> d.setMonth(8)
> d
Mon Oct 01 2012 22:53:50 GMT-0400 (EDT)
// Doing the same thing, changing the day first
> var d = new Date()
> d
Fri Aug 31 2012 22:53:50 GMT-0400 (EDT)
> d.setDate(30)
> d
Thu Aug 30 2012 22:53:50 GMT-0400 (EDT)
> d.setMonth(8)
Sun Sep 30 2012 22:53:50 GMT-0400 (EDT)
So the simple answer is, because the date for today is the 31st of August and the 31st of September is October 1st.