Date Format Issues - javascript

So I have this date returned from an API:
"2014-08-07T00:00Z"
And the results from new Date("2014-08-07T00:00Z") equal Wed Aug 06 2014 20:00:00 GMT-0400 (EDT) and .getDay() on that date gives me 3.
Why is it going from August 7th, to 6th, and the getDay returns 3?
Basically I'm trying to turn the API return date into english.
days[d.getDay()]+", "+months[d.getMonth()]+" "+getOrdinal(d.getDay())+" "+formatAMPM(d)
(aka "Wednesday, August 3rd 8:00 pm")
console.log(d,data[i].startDate, d.getDay());
//yields
// Wed Aug 06 2014 20:00:00 GMT-0400 (EDT) "2014-08-07T00:00Z" 3
days is just an array of text days, as is months, getOrdinal is a function that gives the text st or nd or rd on the day, and formatAMPM is pretty obvious.

.getDay() returns the day of the week. I think you'd expect .getDate()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDate
That's why you're seeing that it is a Wednesday.
The result for me of new Date("2014-08-07T00:00Z") is Wed Aug 06 2014 18:00:00 GMT-0600 (MDT). This is because dates are converted to your local timezone when constructed

I don't think that there is any problem here.
Your date is 2014-08-07T00:00Z, with Z meaning Zulu Time Zone (equivalent to UTC), and using new Date() on it will convert to you local time, here GMT-4, that is why you are getting 4 hours difference.
And For the "3", the getDay() method returns the day of the week, wednesday in your case.

Related

GSheets GS Dates are one day off for 3/9/2020 - 4/5/2020 all other days are correct

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.

moment.js - subtract 1 day from Sunday not working as expected

I have a moment object that I want to subtract 1 day from. The original date shows as Sun Jul 15 2018 12:00:00 and I want to subtract 1 day from it so that it outputs as Sat Jul 14 2018 12:00:00.
This seems like it should be really easy if I use the subtract() function, but it's changing the date to the upcoming Saturday, not the Saturday before July 15. I'm assuming this has something to do with the week starting on July 15.
This seems to only be an issue when I'm using Sunday as my starting date. How can I make this work the way I need it to?
Here is my JS:
var timeFormat = 'dddd h:mma';
var originalDate = moment("sunday 12:00:00pm", timeFormat);
var previousDay = moment(originalDate).subtract(1, 'days').format(timeFormat);
var newDate = moment(previousDay+"12:00:00pm", timeFormat);
$(".openTime span").text(originalDate);
$(".newOpenTime span").text(newDate);
This outputs Sun Jul 15 2018 12:00:00 as the originalDate and Sat Jul 21 2018 12:00:00 as the date subtracted by 1 day. As you can see the new date is now Sat Jul 21 for some reason.
Here's a JSFiddle link: https://jsfiddle.net/dmcgrew/b5ev8knd/22/
The problem is that you use a formatted string to create the newDate. The string says something like this:
'Saturday 12:00:00pm'. MomentJs has no information what Saturday you actually mean, so it just takes the next one, which is the 21st of July.
If you just use the previousDay moment and format it, it works:
https://jsfiddle.net/b5ev8knd/36/

JavaScript two equal Date with different day

I'm using method getDay() from Date() to get day of the week, then I notice that the same Date without time returns me a different day of the week
new Date().getDay()
this first one returns me 3
new Date('2017-09-27').getDay()
this one returns me 2
Comparing these results with some friends I notice that almost everyone had different results, even worse, some people really got day 26 when the date 27 was written
I'm almost sure this problem is caused by timezone issues but I can't find how get accurate results
The reason is when you pass the date 2017-09-27 into the date method you have not passed a timezone in. Look at the results below:
> new Date()
Wed Sep 27 2017 12:58:39 GMT-0700 (PDT)
> new Date('2017-09-27')
Tue Sep 26 2017 17:00:00 GMT-0700 (PDT)
You can pass your time zone in for example in PDT (GMT-08:00)
> new Date('2017-09-27 GMT-08:00')
Wed Sep 27 2017 01:00:00 GMT-0700 (PDT)
Or you can use a library like moment.

Javascript date 01/01/0001

I want to create a Date object in javascript, which represents the year 0001, 2014 years ago.
I tried with
d = new Date();
d.setYear(1);
console.log(d);
but it gives the year 1901
with
d = new Date(1,1,1)
console.log(d);
no way.
How can I create this date?
First of all it's not a Y2K issue at all! ( UPDATE: in some cases - it's related to Y2K issues, but it's not the problem here )
The correct answer is that you can't do that reliably. Does Daylight saving time apply to year 1? How many Leap Years were there? Were there any? Etc. But the answer by #Daniel will use it!
UPDATE: not to mention #MattJohnson post about DST. DST in year 1, actually JS (ES5 anyway) will lie and use the current DST rule for all years
So please don't fool yourself with the idea that you can reliably work with dates lower than 1970. (You will have a lot of problems and surprises even in that time range.)
But if you really, really need to you can use new Date('0001-01-01') (ISO 8601 format) or #Daniel's method:
var d = new Date(); d.setFullYear(1);
But before you use it read this...
There are 4 ways to create a Date in JS:
new Date()
new Date(milliseconds)
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
1) new Date() creates current date ( in your local timezone ) so we are not interested in it for now.
2) new Date(number) creates a new date object as zero time plus the number. Zero time is 01 January 1970 00:00:00 UTC.
So in this case time in JS counting from year 1970.
(new Date(0)).toUTCString()
"Thu, 01 Jan 1970 00:00:00 GMT
If you use negative number you can go "back" it time before 1970
(new Date(-62167219200000)).toUTCString()
"Sat, 01 Jan 0 00:00:00 GMT"
-62167219200000 <- milliseconds
-62167219200000 / 1000
-62167219200 <- seconds
-62167219200 / 60
-1036120320 <- minutes
-1036120320 / 60
-17268672 <- hours
-17268672 / 24
-719528 <- days
-719528 / 365
-1971.309589041096 <- years ( this is roughly calculated value )
The problem is that it's not reliable. How many Leap Years were there? Were there any? Daylight saving time? Etc. And I don't like it because of this magic number -62167219200000
3) new Date(dateString) This is most 'reliable' way. DateString - A string representing an RFC2822 or ISO 8601 date.
RFC2822 / IETF date syntax (RFC2822 Section 3.3), e.g. "Mon, 25 Dec 1995 13:30:00 GMT"
The problem with it is that if you use negative number you will get an incorrect Date with NaN in a result of all methods
(new Date('01 January -1 00:00:00 UTC')).getFullYear()
NaN
If you use year higher or equal 0 and lower then 50 then 2000 will be added automatically.
(new Date('01 January 0 00:00:00 UTC')).getFullYear()
2000
(new Date('01 January 1 00:00:00 UTC')).getFullYear()
2001
(new Date('01 January 10 00:00:00 UTC')).getFullYear()
2010
(new Date('01 January 01 00:00:00 UTC')).getFullYear()
2001
(new Date('01 January 30 00:00:00 UTC')).getFullYear()
2030
(new Date('01 January 49 00:00:00 UTC')).getFullYear()
2049
And if you use year higher or equal 50 and lower then 100 then 1900 will be added.
(new Date('01 January 50 00:00:00 UTC')).getFullYear()
1950
(new Date('01 January 51 00:00:00 UTC')).getFullYear()
1951
(new Date('01 January 90 00:00:00 UTC')).getFullYear()
1990
(new Date('01 January 99 00:00:00 UTC')).getFullYear()
1999
Years equal 100 or higher will get correct year number
(new Date('01 January 100 00:00:00 UTC')).getFullYear()
100
(new Date('01 January 101 00:00:00 UTC')).getFullYear()
101
(new Date('01 January 999 00:00:00 UTC')).getFullYear()
999
(new Date('01 January 9999 00:00:00 UTC')).getFullYear()
9999
So we can't create year 1 using RFC2822 / IETF date syntax
About ISO 8601:
http://www.w3.org/TR/NOTE-datetime
The actual formats are
Year:
YYYY (eg 1997)
Year and month:
YYYY-MM (eg 1997-07)
Complete date:
YYYY-MM-DD (eg 1997-07-16)
Complete date plus hours and minutes:
YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
Complete date plus hours, minutes and seconds:
YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
Complete date plus hours, minutes, seconds and a decimal fraction of a second
YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
We are most interested in 'Complete date'
(new Date('0001-01-01')).toUTCString()
"Mon, 01 Jan 1 00:00:00 GMT"
Yahoo!!! ( or it is better to say Google! :) ), we can use ISO 8601 to create date with year 1
But be careful and do not try to use negative numbers or short year numbers, as parsing of those will may vary on localization or just insanity :)
(new Date('-0001-01-01')).toUTCString()
"Sun, 31 Dec 2000 21:00:00 GMT"
(new Date('01-01-01')).toUTCString()
"Sun, 31 Dec 2000 21:00:00 GMT"
(new Date('02-01-01')).toUTCString()
"Wed, 31 Jan 2001 21:00:00 GMT"
(new Date('02-01-05')).toUTCString()
"Mon, 31 Jan 2005 21:00:00 GMT"
4) new Date(year, month, day, hours, minutes, seconds, milliseconds)
To use this one you have to pass two parameters ( year, and month ), all other will be optional. Be careful because here month will start from 0 to 11, not like everywhere else. WAT? o_O
WARNING! This date will be created in your current time zone!!! So be careful using it!
UPD: clarification by #matt-johnson
...actually the Date object always reflects the local time zone. You can't place it in another time zone, and even if you initialize it with a UTC timestamp, it will still reflect back the local time zone in most of the functions. Internally it tracks UTC by the numeric timestamp, and there are functions that expose UTC values explicitly, but everything else is local.
The negative numbers will be interpreted as negative years
(new Date(-1, 0)).toString()
"Fri Jan 01 -1 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
(new Date(-234, 0)).toString()
"Wed Jan 01 -234 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
The numbers from 0 to 99 will be incremented by 1900 automatically
(new Date(0, 0)).toString()
"Mon Jan 01 1900 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
(new Date(1, 0)).toString()
"Tue Jan 01 1901 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
(new Date(11, 0)).toString()
"Sun Jan 01 1911 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
(new Date(50, 0)).toString()
"Sun Jan 01 1950 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
(new Date(99, 0)).toString()
"Fri Jan 01 1999 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
The numbers from 100 to 275760 will be interpreted as year numbers
(new Date(100, 0)).toString()
"Fri Jan 01 100 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
(new Date(102, 0)).toString()
"Sun Jan 01 102 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
(new Date(2002, 0)).toString()
"Tue Jan 01 2002 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
And numbers higher then 275760 will be Invalid date
(new Date(275760, 0)).toString()
"Tue Jan 01 275760 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
(new Date(275761, 0)).toString()
"Invalid Date"
UPD:
new Date(Date.UTC(1,1,1)) will safer from same symptoms as new Date(year, month, day, hours, minutes, seconds, milliseconds). Because of Date.UTC function.
Use setFullYear - this is the Y2K issue with JavaScript.
var d = new Date(); d.setFullYear(1);
document.write(d);
You can use the string version of the constructor:
console.log(new Date("0001-01-01"));
The (deprecated) Date.setYear method sets the year for a specified date, but note that:
If yearValue is a number between 0 and 99 (inclusive), then the year
for dateObj is set to 1900 + yearValue. Otherwise, the year for
dateObj is set to yearValue.
var theBigDay = new Date();
theBigDay.setYear(96); // sets year to 1996
theBigDay.setYear(1996); // sets year to 1996
theBigDay.setYear(2000); // sets year to 2000
You should use Date.setFullYear method nevertheless. The other method is deprecated.
when handling JavaScript date objects you have to specify the full year for ex, 1950 and not 50. It is mentioned in the doc. So the code should be as Daniel has said also,
d = new Date(); d.setFullYear(1);
console.log(d);
Actually, you can use the Date constructor for years whose absolute value is below 100, by using the negative year instead of the positive one, and then setting it back to positive using the reliable .setFullYear() / .setUTCFullYear() methods. This is because the issue of getting 20th or 21st century years in the case of historical ones manifests itself only for under 100 positive years. While for a basic usage this works without problems, care should be taken when using the new Date(...) constructor to add or subtract time at object creation moment, since you'd want to switch the operation sign accordingly as well. Also, if the desired historical year is negative in the first place and you need to add or subtract from it at construction time, the matter suffers further complications.
Therefore, to avoid any confusion and be clear and consistent about things, using the extended form of .setFullYear() / .setUTCFullYear() and .setHours() / .setUTCHours() to set the entire date and time parts after the object construction should be an optimal alternative and present no drawbacks whatsoever, while also being similar to setting things at construction time.
Below there are examples for each of the two methods mentioned above and both local and UTC dates (an optional 365 day or 31536000000 ms year is "added" in all cases, to illustrate if and how it changes things):
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Historic Dates Problem</h2>
<p>Construct negative dates and switch them to positive (1) or <br>Set the full date and time parts separately (2)</p>
<p id="examples"></p>
<script>
var loc1 = new Date(- 37, 11, 24, 10, 33, 30, - 31536000000); loc1.setFullYear(- loc1.getFullYear());
var utc1 = new Date(Date.UTC(- 37, 11, 24, 10, 33, 30, - 31536000000)); utc1.setUTCFullYear(- utc1.getUTCFullYear());
var loc2 = new Date(); loc2.setFullYear(37, 11, 24); loc2.setHours(10, 33, 30, 31536000000);
var utc2 = new Date(); utc2.setUTCFullYear(37, 11, 24); utc2.setUTCHours(10, 33, 30, 31536000000);
document.getElementById("examples").innerHTML = "LOC1: " + loc1 + "<br>UTC1: " + utc1 + "<br>LOC2: " + loc2 + "<br>UTC2: " + utc2;
</script>
</body>
</html>

javascript: new date, how to set default year?

I have a set of dates w/ or wo/ year, like the following:
10/1/2010
10/1
1-Oct
1-Oct 2006
Jan 4
Jan 4, 2008
When I tried to parse them using new Date(),
for those date that don't have year, I got year as 2001
e.g.
a=new Date("Jan 4")
Thu Jan 04 2001 00:00:00 GMT-0500 (EST)
I want to parse all the dates. If they have year, use that year(2010, 2006, 2008 in the above dates). otherwise, use 2011
Anyone can help?
Thanks,
Cheng
Use DateJS for parsing dates. The Date constructor's parser is very limited.

Categories

Resources