convert date string to ISO date with timezone - javascript

I have a collection of dates and times, it is formatted like this:
01.07.2013 16:10.
I know I have to rearrange to match ISO standard, so I managed to end up with a string like this:
2013-07-01T16:10.
From this, I need to create a Date Object. After reading many questions on here about this I am a little confused about how to add a timezone to this. All of the times are in New York Cities local time, which I also want to store the dates in.
So since Javascript takes the timezone from my machine, I changed it to be the one from NYC. However, the following part confuses me the most:
> print(new Date("2013-07-01T16:10"))
Mon Jul 01 2013 16:10:00 GMT+2000 (EDT)
Question: Why does it say GMT+2000 when my local time is GMT-0400 (the one of NYC)? Is this 'correct'? If not, how is this done properly?
--Updates:
I am using mongoDB 3.4.4 and interpreter Version is MozJS-38. The systemsetup -gettimezone returns America/New_York. Running mongo-express in chrome shows: Mon Jul 01 2013 06:00:00 GMT+0200 (EDT) for ISODate("2013-07-01T04:00:00.000Z") stored, but the shell says Tue Jul 02 2013 00:00:00 GMT+2000 (EDT) for print(new Date("2013-07-01T04:00:00.000Z"));.

You didn't specify the time zone offset when creating the Date object.
> new Date("2013-07-01T16:10-04:00")
2013-07-01T20:10:00.000Z

Related

Getting the value from momentJS subtract without a time zone or offset (GMT +0000)

I wish to produce a date 30 days in the past using momentJS - it's pretty easy, I just use the following
const date30DaysPast = moment().utc().subtract(30, 'days').toDate(); // Sun Oct 29 2017 13:23:46 GMT+0100 (CET)
This is all great however I want the returned date to have no Time Zone or offset, I want the time to be GMT+0000 not as above GMT+0100 (CET) - for example:
Sun Oct 29 2017 13:23:46 GMT+0000
I wish to force this as I am using testing servers that are in different locations, and rather than take the time from the local browser I just want set a standard time. I thought using the utc method would do this for example should I write something like this in my test:
const oct4th2017 = moment.utc(new Date('October 04, 2017 11:13:00'));
the output is
Wed Oct 04 2017 09:13:00 GMT+0000
How can I remove the offset / time zone and set it to GMT on my original subtract method? I have tried wrapping in a parent utc method like so
const date30DaysPast = moment.utc(moment().utc().subtract(30, 'days').toDate());
but this doesn't work. I get the momentJS object.
Any advice could be appreciated, should my wording be bad or confusing please say so and I shall reword my question.
You already have your moment.js object without timezone. But the toDate() method creates a native Date object, which always has the local timezone (though it has various UTC methods).

convert month string to date

I'm using some library that won't sort objects by a string value but will sort them by date. I have months like '2008-04' and I should be able to convert them to Javascript dates for the first of the appropriate month. But my local timezone screws things up:
new Date('2008-04')
Mon Mar 31 2008 20:00:00 GMT-0400 (EDT)
This is probably a duplicate of How do you convert a JavaScript date to UTC?, but maybe there's a simpler answer for my particular use case than the ones there?
BTW, I get the same answer by specifying the first of the month:
new Date('2008-04-01')
Mon Mar 31 2008 20:00:00 GMT-0400 (EDT)
I'm using ES6. I don't suppose that makes it any more straightforward?
Add '-01T00:00:00Z' to the string with part of ISO 6801 date:
document.write(new Date('2008-04' + '-01T00:00:00Z'));
I misunderstood. The date is already UTC, it's just when I display it as a string locally that it gets converted to my local timezone. So the answer is just
new Date('2008-04').toUTCString()
"Tue, 01 Apr 2008 00:00:00 GMT"
or
new Date('2008-04').toISOString()
"2008-04-01T00:00:00.000Z"

Why does js subtract a day from a Date object with a certain format?

I get dates from the database in this format:
yyyy-mm-dd
When I create a javascript Date object using this string, it builds a day before the date.
You can test this in your console:
var d = new Date("2015-02-01");
d
You will get January 31st! I've tested many theories, but none answer the question.
The day is not zero-based, otherwise it would give Feb 00, not Jan 31
It's not performing a math equation, subtracting the day from the month and/or year
Date(2015-02-01) = Wed Dec 31 1969
Date("2015-01") = Wed Dec 31 2014
It is not confusing the day for the month
Date("2015-08-02") = Sat Aug 01 2015
If this were true the date would be Feb 08 2015
If you create a Date using a different format, it works fine
Date("02/01/2015") = Feb 1st, 2015
My conclusion is that js does this purposefully. I have tried researching 'why' but can't find an explanation. Why does js build dates this way, but only with this format? Is there a way around it, or do I have to build the Date, then set it to the next day?
PS: "How to change the format of the date from the db" is not what I'm asking, and that is why I'm not putting any db info here.
Some browsers parse a partial date string as UTC and some as a local time,
so when you read it the localized time may differ from one browser to another
by the time zone offset.
You can force the Date to be UTC and add the local offset if you
want the time to be guaranteed local:
1. set UTC time:
var D= new Date("2015-02-01"+'T00:00:00Z');
2. adjust for local:
D.setMinutes(D.getMinutes()+D.getTimezoneOffset());
value of D: (local Date)
Sun Feb 01 2015 00:00:00 GMT-0500 (Eastern Standard Time)
Offset will be whatever is local time.
Some differences between browsers when time zone is not specified in a parsed string:
(tested on Eastern Standard Time location)
(new Date("2015-02-01T00:00:00")).toUTCString();
Firefox 35: Sun, 01 Feb 2015 05:00:00 GMT
Chrome 40: Sun, 01 Feb 2015 00:00:00 GMT
Opera 27: Sun, 01 Feb 2015 00:00:00 GMT
IE 11: Sun, 01 Feb 2015 05:00:00 GMT
IE and Firefox set the Date as if it was local, Chrome and Opera as if it was UTC.
In javascript, Date objects are internally represented as the number of milliseconds since Jan 1st 1970 00:00:00 UTC. So instead of thinking of it as a "date" in the normal sense, try thinking of a Date object as a "point in time" represented by an integer number (without timezone).
When constructing your Date object using a string, you are actually just calling the parse function. Most date time formats (including ISO 8601) allow you to reduce the precision of a date string.
For reduced precision, any number of values may be dropped from any
of the date and time representations, but in the order from the least
to the most significant.
e.g. 2015-02-01 would represent the day February 1st 2015.
This causes a dilemma for javascript because a Date object is always accurate to the millisecond. Javascript cannot store a reduced accuracy date since it is just an integer of milliseconds since 1st Jan 1970. So it does the next best thing which is to assume a time of midnight (00:00:00) if not specified, and a timezone of UTC if not specified.
All valid javascript implementations should give the same result for this:
var d = new Date("2015-02-01");
alert(d.getTime());
1422748800000
The out-by-1-day issue comes when outputting the date either to some (often unclear) debugger or using the getter methods because the local timezone is used. In a browser, that will be your operating systems timezone. Anyone "west" of Greenwich Mean Time may see this problem because they have a negative UTC offset. Please note there are UTC equivalent functions too which use the UTC timezone, if you are really just interested in representing a date rather than a point in time.

Timezone offset causing wrong date

I'm using Bootstrap DatePicker's setEndDate function (Reference).
I need to pass to it a Javascript date object. I'm trying to convert a simple year-month-day string to a date, but depending on the windows' timezone, I get different results:
In GMT -8:
> new Date('2015-01-16')
=> Thu Jan 15 2015 16:00:00 GMT-0800 (Pacific Standard Time)
In GMT +2:
> new Date('2015-01-16')
=> Fri Jan 16 2015 02:00:00 GMT+0200 (Eastern Europe Standard Time)
So what I'm left with is a different end-date depending on what timezone the user is in, even though the server side provided the same date string.
I will appreciate help on the matter.
Apparently, even though the documentation states the function's argument should be a date object, it can in fact be given a string in a "m-d-yyyy" format.
So in my case all I had to do was call the function like this:
$('#my-input').datepicker('setEndDate', '1-16-2015');
Hope this helps someone.

Javascript date formatting - one hour out due to daylight saving

So now, its 9:23am. I have a UTC date string that represents the current date, that looks like this "2012-07-17T09:23:27.75"
I want that in a date object, so I can display a nicely formatted date, so I:
var myDate = new Date("2012-07-17T09:23:27.75")
// Gives --> Tue Jul 17 2012 10:23:27 GMT+0100 (GMT Daylight Time)
So because of daylight saving time I'm getting an hour-out issue. I can see that myDate.getTimezoneOffset() gives me -60, what's the standard / best practice way to get my date to actually reflect the current correct time? Have I just entered javascript date hell?
Try momentjs.com. I really found it handy for such things.
var myDate = moment("2012-07-17T09:23:27.75");
Gives you a date instance in your timezone (that basically configured on your computer). Moreover momentjs has nice human friendly formattings like "a couple of seconds ago", "a month ago",...
Dates are really a hell in JS (but not only in JS). The best thing you can do is to always only transport in UTC between browser <-> server. Then on the server convert it to what time format you like, you obviously only have to be consistent. That way I managed to handle date-times properly.
Try removing the 'T'
I was debugging some date time format issue in chrome when I found out that in console
new Date('2016-04-16T15:15:00') returns Sat Apr 16 2016 16:15:00 GMT+0100 (GMT Daylight Time)
while
new Date('2016-04-16 15:15:00') returns Sat Apr 16 2016 15:15:00 GMT+0100 (GMT Daylight Time)

Categories

Resources