I'm trying to create a date in Javascript, but it is adding one day.
sDate = new Date('2020-04-01');
console.log(sDate);
The console output is: Date Tue Mar 31 2020 19:00:00 GMT-0500 (Central Daylight Time)
Why isn't it April 1st?
Thanks
Related
I ran into a problem with a 'time' in JS. So basicaly I'm tring to get a time from databace as a string like 11:00 and 20:30.
With the upcoming function code I convert it to js format:
function getDateFromHours(time) {
time = time.split(':');
let now = new Date();
return new Date(now.getFullYear(), now.getMonth(), now.getDate(), ...time);
}
After all I got this: Mon Nov 09 2020 11:00:00 GMT+0300 and this: Mon Nov 09 2020 20:30:00 GMT+0300
So the first question:
How to compare these times to each other?
How to output every 15 minuties between these two times in concole to make it look like:
Mon Nov 09 2020 11:00:00 GMT+0300
Mon Nov 09 2020 11:15:00 GMT+0300
Mon Nov 09 2020 11:30:00 GMT+0300
etc...
For comparing times, and generating times algorithmically, you're best to work in timestamps. It's just a matter of converting between milliseconds and whatever unit of time you care to think in. You can get the timestamp from a Date object with the getTime method.
GS code comes back one day off when getting date from 3/9/2020 - 4/5/2020
All dates between 3/9/2020 - 4/5/2020 comeback incorrect.
Google sheet add date column with date 3/9/2020
Add code to gs below
Comes back: Sun Mar 08 2020 23:00:00 GMT-0600 (CST)
3/9/2020
Sun Mar 08 2020 23:00:00 GMT-0600 (CST)
4/5/2020
Sat Apr 04 2020 23:00:00 GMT-0600 (CST)
var data = SpreadsheetApp.getActiveSpreadsheet().getDataRange().getValues();
SpreadsheetApp.getUi().alert(data[0][0]);
Here is the google sheet: link
The dates come back correctly if I format it using GMT.
var formattedDate = Utilities.formatDate(data[0][0], "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'");
UTC, GMT and Daylight Saving Time
Neither UTC nor GMT ever change for Daylight Saving Time (DST). However, some of the countries that use GMT switch to different time zones during their DST period.
For example, the United Kingdom is not on GMT all year, it uses British Summer Time (BST), which is one hour ahead of GMT, during the summer months.
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.
I have just noticed some strange behavior in one of my Arrays. I am sure the issue is with how Javascript stores object references in arrays. I will demonstrate the issue with a bit of code I posted as an answer to another question on SO. The code, just loops to get todays date and the 6 previous dates of the month, pretty self explanatory.
var dates = [];
var date = new Date();
for (var i = 0; i < 7; i++){
var tempDate = new Date();
tempDate.setDate(date.getDate()-i);
dates.push(tempDate);
}
console.log(dates);
Output: [Thu Jun 05 2014 14:54:14 GMT+0100 (GMT Daylight Time),
Wed Jun 04 2014 14:54:14 GMT+0100 (GMT Daylight Time),
Tue Jun 03 2014 14:54:14 GMT+0100 (GMT Daylight Time),
Mon Jun 02 2014 14:54:14 GMT+0100 (GMT Daylight Time),
Sun Jun 01 2014 14:54:14 GMT+0100 (GMT Daylight Time),
Sat May 31 2014 14:54:14 GMT+0100 (GMT Daylight Time),
Fri May 30 2014 14:54:14 GMT+0100 (GMT Daylight Time)]
This is correct, and is expected, as tempDate is continually recreated as a new Date object inside of the loop.
When I take tempDate out of the loop however, it seems to update all of the objects in the array every iteration of the loop (also the loop seems to go one Month and 1 day too far to 29th apr):
var dates = [];
var date = new Date();
var tempDate = new Date();
for (var i = 0; i < 7; i++){
tempDate.setDate(date.getDate()-i);
dates.push(tempDate);
}
console.log(dates);
Output: [Tue Apr 29 2014 14:52:21 GMT+0100 (GMT Daylight Time),
Tue Apr 29 2014 14:52:21 GMT+0100 (GMT Daylight Time),
Tue Apr 29 2014 14:52:21 GMT+0100 (GMT Daylight Time),
Tue Apr 29 2014 14:52:21 GMT+0100 (GMT Daylight Time),
Tue Apr 29 2014 14:52:21 GMT+0100 (GMT Daylight Time),
Tue Apr 29 2014 14:52:21 GMT+0100 (GMT Daylight Time),
Tue Apr 29 2014 14:52:21 GMT+0100 (GMT Daylight Time)]
So the two questions I pose are:
Why does the object stored in the array keep mutating with every
iteration? (I suspect this is because of the way Javascript is
storing the references to the object, but an explanation would be
nice)
Why does the loop run one too far in the second bit of code? The first example shows the loop ends after 7 successful iterations, on May the 30th (7 days in the past), the second example shows the resultant date after the iterations as the 29th april - more than a month into the past. Why?
edit: I have a rudimentary jsfiddle, to allow testing the code.
var tempDate = new Date(); creates new object. so in this example:
var array = [tempDate,tempDate,tempDate,tempDate,tempDate];
array[0] is same object as array[1]
this is just how objects works in almost every programming language:
var a = tempDate;
var b = tempDate;
var c = b;
(in above example a, b, c, and tempDate is same thing)
Regarding question 2:
please have a look at this fiddle http://jsfiddle.net/Mqnmm/
with new object tempDate is always today, then you apply setDate()
with old object, before last literation date is Sat, 31 May 2014 14:23:34 GMT, then you apply tempDate.setDate(-1) which sets date to last N days of previous month
Subtract days from a date in JavaScript please check Rob Dawley`s answer to properly adjust dates
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.