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);
Related
I am getting some weird output for certain dates for moment js.
It is only for the dates 1st, 2nd and 3rd of Jan 2022 otherwise it works as expected.
I would expect the same output regardless of the current date.
Any idea why this is occurring?
console.log('Test 2019', moment().year(2019).week(48).endOf("week"));
console.log('Test 2020', moment().year(2020).week(48).endOf("week"));
console.log('Test 2021', moment().year(2021).week(48).endOf("week"));
console.log('Test 2022', moment().year(2022).week(48).endOf("week"));
console.log('Test 2023', moment().year(2023).week(48).endOf("week"));
// With date of device set to the future 3/Jan/2022
Test 2019 Sun Dec 09 2018 23:59:59 GMT+1300 (New Zealand Daylight Time)
Test 2020 Sun Dec 08 2019 23:59:59 GMT+1300 (New Zealand Daylight Time)
Test 2021 Sun Dec 06 2020 23:59:59 GMT+1300 (New Zealand Daylight Time)
Test 2022 Sun Dec 04 2022 23:59:59 GMT+1300 (New Zealand Daylight Time)
Test 2023 Sun Dec 03 2023 23:59:59 GMT+1300 (New Zealand Daylight Time)
// With date of device set to the today 15/dec/2021
Test 2019 Sun Dec 08 2019 23:59:59 GMT+1300 (New Zealand Daylight Time)
Test 2020 Sun Dec 06 2020 23:59:59 GMT+1300 (New Zealand Daylight Time)
Test 2021 Sun Dec 05 2021 23:59:59 GMT+1300 (New Zealand Daylight Time)
Test 2022 Sun Dec 04 2022 23:59:59 GMT+1300 (New Zealand Daylight Time)
Test 2023 Sun Dec 03 2023 23:59:59 GMT+1300 (New Zealand Daylight Time)
Updated test:
console.log('Test 2019 B', moment().year(2019));
console.log('Test 2020 B', moment().year(2020));
console.log('Test 2021 B', moment().year(2021));
console.log('Test 2022 B', moment().year(2022));
console.log('Test 2023 B', moment().year(2023));
// With date of device set to auto / today 15/dec/2021
Test 2019 B Sun Dec 15 2019 15:14:54 GMT+1300 (New Zealand Daylight Time)
Test 2020 B Tue Dec 15 2020 15:14:54 GMT+1300 (New Zealand Daylight Time)
Test 2021 B Wed Dec 15 2021 15:14:54 GMT+1300 (New Zealand Daylight Time)
Test 2022 B Thu Dec 15 2022 15:14:54 GMT+1300 (New Zealand Daylight Time)
Test 2023 B Fri Dec 15 2023 15:14:54 GMT+1300 (New Zealand Daylight Time)
// With date of device set to the future 3/Jan/2022
Test 2019 B Thu Jan 03 2019 15:21:48 GMT+1300 (New Zealand Daylight Time)
Test 2020 B Fri Jan 03 2020 15:21:48 GMT+1300 (New Zealand Daylight Time)
Test 2021 B Sun Jan 03 2021 15:21:48 GMT+1300 (New Zealand Daylight Time)
Test 2022 B Mon Jan 03 2022 15:21:48 GMT+1300 (New Zealand Daylight Time)
Test 2023 B Tue Jan 03 2023 15:21:48 GMT+1300 (New Zealand Daylight Time)
console.log('Test 2019 C', moment().year(2019).week(48));
console.log('Test 2020 C', moment().year(2020).week(48));
console.log('Test 2021 C', moment().year(2021).week(48));
console.log('Test 2022 C', moment().year(2022).week(48));
console.log('Test 2023 C', moment().year(2023).week(48));
// With date of device set to the future 3/Jan/2022
Test 2019 C Thu Dec 06 2018 15:26:35 GMT+1300 (New Zealand Daylight Time)
Test 2020 C Fri Dec 06 2019 15:26:35 GMT+1300 (New Zealand Daylight Time)
Test 2021 C Sun Dec 06 2020 15:26:35 GMT+1300 (New Zealand Daylight Time)
Test 2022 C Mon Nov 28 2022 15:26:35 GMT+1300 (New Zealand Daylight Time)
Test 2023 C Tue Nov 28 2023 15:26:35 GMT+1300 (New Zealand Daylight Time)
I'm setting up a Graph using CanvasJS. Everything working fine. But in dates array, if there are no dates between from date object date: 23-7-2019 09:00 AM, value: 20 to date object date: 13-10-2019 11 AM, value: 25 the graph showing a straight line between these to dates. So I want to add dates for each day between those dates and want to set the value as null. so that graph will show the empty gaps instead of straight line.
So earlier I added value as null if the values or negative values. I have added the code below.
var mydatapoints4 = [];
for(var k=0; k< list_values.length; k++){
var tk= list_values[k].x;
var uk = list_values[k].y;
if(uk > 0){
mydatapoints4.push({x: new Date(tk), y: uk});
}
else{
mydatapoints4.push({x: new Date(tk), y: null});
}
};
So My datapoints 4 array is like this
0: {x: Fri Apr 06 2018 22:30:00 GMT-0500 (Central Daylight Time), y: 93.9}
1: {x: Sat Apr 07 2018 09:00:00 GMT-0500 (Central Daylight Time), y: 69.3}
2: {x: Tue Apr 10 2018 10:00:00 GMT-0500 (Central Daylight Time), y: 71.2}
3: {x: Wed Apr 11 2018 11:00:00 GMT-0500 (Central Daylight Time), y: 67.2}
I want the output of the array like this.
0: {x: Fri Apr 06 2018 22:30:00 GMT-0500 (Central Daylight Time), y: 93.9}
1: {x: Sat Apr 07 2018 09:00:00 GMT-0500 (Central Daylight Time), y: 69.3}
2: {x: Sun Apr 08 2018 09:00:00 GMT-0500 (Central Daylight Time), y: null}
3: {x: Mon Apr 09 2018 09:00:00 GMT-0500 (Central Daylight Time), y: null}
4: {x: Tue Jun 10 2018 10:00:00 GMT-0500 (Central Daylight Time), y: 71.2}
5: {x: Wed Jun 11 2018 11:00:00 GMT-0500 (Central Daylight Time), y: 67.2}
I hope my question is clear to you, and may I know how can I do that? if you can help me it's a huge help to me.
Thanks for the support.
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)) {
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.
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