I need to getFullYear on a JavaScript Date, however newing up a Date() creates it at UTC, and in the cases of January 1, it becomes LAST YEAR. (at my timezone, EST)
so:
startDateYear = new Date('2019-01-01').getFullYear()
returns 2018
I know .getISODate() does get the true date in my timezone, but I need to come up with a way to chain .getISODate() with .getFullYear() to get the true year.
Related
Why this sample give me 22 number instead client 23 number:
console.log(new Date().toISOString());
Output is: 2019-01-22T22:58:46.606Z
Client side time:
The toISOString() method always outputs the time in UTC. From the docs:
The timezone is always zero UTC offset, as denoted by the suffix "Z".
In this case, the date is still the 22nd in UTC but, in your timezone, it's already the 23rd.
The Date object you have here is still in your local timezone, though. It just happens that the toISOString() method always outputs a UTC representation. If you do the following, you should see the date you're expecting:
console.log(new Date().toLocaleString()) // "1/22/2019, 3:14:18 PM" for me (US Pacific Time)
toISOString() returns a date/time in ISO format with timezone as UTC +0.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
Just to add on to what's already been offered. You still need to be careful about using the Date() method. It can generate different dates depending on what you give it.
//This outputs midnight (12:00AM) on January 22nd in the UTC timezone
new Date('2019-01-22').toISOString() // output: "2019-01-22T00:00:00.000Z"
//This outputs 5:00AM on January 22nd in the UTC timezone because my PC is in the
//Eastern Standard timezone and I used a different format for my date string.
new Date('01/22/2019').toISOString() // output: "2019-01-22T05:00:00.000Z"
Javascript Dates work with timezones. If I create a date, it sets the timezone. When I update the year of that date, I don't expect the timezone to change. It does, however. The worst thing is that it changes the timezone but not the time, causing the actual time to shift by one hour!
This causes the issue that if I have the person's birth date, and want to know his birthday this year, I cannot simply set the year to the current year using birthdate.setFullYear(2018), because it will return the birthday minus one hour. That means that it occurs one day before the actual birthday, at eleven 'o clock.
let test = new Date('1990-10-20');
console.log(test);
console.log(test.toISOString().substring(0, 10));
// 1990-10-20 ( Sat Oct 20 1990 01:00:00 GMT+0100 (Central European Standard Time) )
test.setFullYear(2000);
console.log(test);
console.log(test.toISOString().substring(0, 10));
// 2000-10-19 ( Fri Oct 20 2000 01:00:00 GMT+0200 (Central European Summer Time) === one hour too soon!! )
It might be that your timezone does not reproduce, here is my output:
"1990-10-20T00:00:00.000Z"
1990-10-20
"2000-10-19T23:00:00.000Z"
2000-10-19
The only workaround I found is substring the date and replace it as string values. How can I do it better using the Date object?
Hm, maybe it would be better to use setUTCFullYear(...) (MDN Docs) instead? At least in that case, it won't mess up the time.
let test = new Date('1990-10-20');
console.log(test);
console.log(test.toISOString().substring(0, 10));
// "1990-10-20T00:00:00.000Z")
test.setUTCFullYear(2000);
console.log(test);
console.log(test.toISOString().substring(0, 10));
// "2000-10-20T00:00:00.000Z"
BEWARE THE TIMEZONE If you want to work with just-a-date using the date object (and you more or less have to) your date objects should represent UTC midnight at the start of the date in question. In your case, to indicate a year, use UTC midnight at the start of the 1st January. This is a common and necessary convention, but it requires a lot of attention to make sure that the timezone doesn't creep back in, as you've discovered.
When you say "javascript dates work with timezones", a javascript date is a moment in time (ticks since the epoch) with handy static functions for converting that moment into a meaningful string in the local timezone (or a specified timezone). The date object itself does not have a timezone property.
So, you can create your UTC midnight year with something like...
var myDate = new Date(Date.UTC(2018,0,1)); // months are zero-indexed !
Then serialize it specifying UTC...
myDate.toISOString();
myDate.toLocaleDateString("en",{timezone:"UTC"});
This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 5 years ago.
I am creating a new date from string
var s = "2017-12-06"
var dt = new Date(s)
console.log(dt) // outputs Tue Dec 05 2017 19:00:00 GMT-0500 (EST)
What am I missing ?
Date.toString() is formatted in your local time zone, but because you've passed in an ISO-8601 string, the value is parsed as if it's UTC.
From the Date.parse() documentation (as the Date(String) constructor is documented to behave like Date.parse):
The date time string may be in a simplified ISO 8601 format. For example, "2011-10-10" (just date) or "2011-10-10T14:48:00" (date and time) can be passed and parsed. Where the string is ISO 8601 date only, the UTC time zone is used to interpret arguments. If the string is date and time in ISO 8601 format, it will be treated as local.
So you'll end up with a Date which is equivalent to 2017-12-06T00:00:00Z. But Date.toString() shows you that instant in time in your current time zone - and if you're in America/New_York or a similar time zone which is 5 hours behind UTC at that moment in time, that means it'll print December 5th at 7pm.
I have a date string that looks like this: 2014-07-21T12:55:31.513Z. Is there some simple way to convert this to a date? I found that there is Date.parse() however that dives me milliseconds since Jan. 1st, 1970 which it doesn't look like that fits my needs.
new Date("2014-07-21T12:55:31.513Z")
You should also have a look here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
use
new Date('2014-07-21T12:55:31.513Z')
the ISO format:
YYYY-MM-DD
or
YYYY-MM-DDTHH:MM:SS
refer Date
Note: Where Date is called as a constructor with more than one
argument, if values are greater than their logical range (e.g. 13 is
provided as the month value or 70 for the minute value), the adjacent
value will be adjusted. E.g. new Date(2013,13,1) is equivalent to new
Date(2014,1,1), both create a date for 2014-02-01 (note that the month
is 0-based). Similarly for other values: new Date(2013,2,1,0,70) is
equivalent to new Date(2013,2,1,1,10) which both create a date for
2013-03-01T01:10:00.
Not sure if this is the best way but I would just parse the string to isolate the year, month, day, hours, minutes, seconds and milliseconds the use the following function:
var date = new Date(year, month, day, hours, minutes, seconds, milliseconds);
Can anyone explain why getFullYear does not return 2014?
console.log(new Date('2014-01-01').getFullYear()) //2013
console.log(new Date('2014-01-01').getUTCFullYear()) //2014
From MDN:
The dateString of "March 7, 2014" returns a different date than "2014-03-07" unless the local time-zone is UTC. When converting a dateString of "March 7, 2014" the local time-zone is assumed. When converting a dateString of "2014-03-07" the UTC time-zone is assumed. This results in two different Date values depending on the format of the string that is being converted.
So when you ask it to parse "2014-01-01", you're getting the time in UTC.
Then you call .getFullYear() on your object, which uses local time. If you live in the Eastern US like I do, then it basically subtracts 4 hours from the internal time and returns the year.
So here's what happens:
"2014-01-01" is converted to "1388534400000"
.getFullYear() is called and "1388534400000" is converted to local time
Local time is something like "1388534160000"
New years hasn't yet occurred at "1388534160000", so it's still 2013
All of this implies that if we do something like
console.log(new Date('January 1, 2014').getUTCFullYear()); // 2014
console.log(new Date('January 1, 2014').getFullYear()); // 2014
We'll get the same year, because we told the browser to use our timezone right on New Year's, but they're not equivalent:
console.log(new Date('January 1, 2014').getUTCHours()); // 5
console.log(new Date('January 1, 2014').getHours()); // 0
According to this:
"The difference is when you specify a string in the format YYYY-MM-DD, you get a date that is 12am in the GMT timezone and when you specify a date in the format DD-MM-YYYY, you get a date that is 12am in your current timezone."
So basically since you are specifying News Years Day 2014 when it gets converted from GMT to your local time it believes it is 12-31-13 not 01-01-14.