Formatting and parsing a date string without timezone conversion - javascript

If I'm in a timezone a few hours before midnight and I try
new Date("2020-03-30T00:00:00.000Z")
it will result in
Sun Mar 29 2020 18:00:00 GMT-0600 (Central Standard Time)
then I realized even
new Date("2020-03-30")
in the wrong timezone will result in the exact same conversion. I have a similar problem with moment.js constructed from similar strings in similar timezones, when I try to call the .format() method it will do a conversion first, sometimes resulting in the wrong day of the month.
How can I format such a date initialized like this and not have to deal with timezone conversions? eg work with just the "raw" date it was initialized from

Related

Date weird timezone (GMT+0009)

I use ExcelJS to parse an Excel into a JavaScript Object.
Here is an excel sample :
As you can see, I must cover multiple format for date and hour. For the date I have no problem. But when it comes to hour, cells with "12:30" and "15:00" value are returned as javascript date object. So I check if its a date or a string and I want to get the string value (10H10 and 18h30 cells are ok because they are considered as string but not the 15:00 and 12:30).
When using VSCode debug tool, I can see that I have this date :
'Sat Dec 30 1899 12:39:21 GMT+0009 (Central European Standard Time)'
'Sat Dec 30 1899 15:09:21 GMT+0009 (Central European Standard Time)'
What I want is a way to cast this result in a string "12:30" or "15:30". I tried many things using moment and Date functions like .toUTCString(), .toGMTString()...
I'm going to assume you have a JS Date object.
Intl.DateTimeFormat would be recommended for formatting date and/or times for users.
But since you want a specific format, it might be easier to format it yourself.
But the time isn't in the correct time zone. You'd first have to convert it to the correct time zone. Except the correct time zone is UTC, and we can access that info about the components of the UTC representation of the date-time without doing any conversion.
[
dt.getUTCHours(),
dt.getUTCMinutes().toString().padStart(2, '0'),
].join(":")

Convert date from UK GMT string to local date in JavaScript

I have a situation where I am always returned the date from the server as a UK date time string.
E.g. '2020-07-19 16:40:00'
This would be 4:40PM in UK at +01:00, or 3:40PM UTC.
I want to be able to convert this time from GMT to the local time on the computer;
If I do this when in the UK...
var date = new Date('2020-06-19 16:40:00 GMT');
it returns Fri Jun 19 2020 17:40:00 GMT+0100 (British Summer Time)
Which is an hour out.
If I do the date in winter time (without daylight savings), this is correct.
var date = new Date('2020-01-19 16:40:00 GMT');
returns Sun Jan 19 2020 16:40:00 GMT+0000 (Greenwich Mean Time)
Is there a way I can correctly adjust this to always give the correct time regardless of what timezone the computer is set in, based on UK clock times.
Thanks in advance
As I understand your question, you don't know if the timestamp from the server is GMT or BST as the offset isn't included. You can work it out using plain JS but it's somewhat kludgy and error prone, see Calculate Timezone offset only for one particular timezone.
It would be much better to get the server to use an ISO 8601 format supported by ECMAScript and either send the offset or always use UTC/GMT.
If that isn't an option, you can use a library like Luxon to specify the location (and hence offset rules) to use for parsing, e.g.
let DateTime = luxon.DateTime;
['2020-07-19 16:40:00', // BST +1
'2020-01-19 16:40:00' // GMT +0
].forEach(ts => console.log(
DateTime.fromFormat(ts, 'yyyy-LL-dd HH:mm:ss', {zone: 'Europe/London'}))
);
<script src="https://cdn.jsdelivr.net/npm/luxon#1.24.1/build/global/luxon.min.js"></script>
PS Don't forget to always tell the parser the format to parse.

Javascript date object automatically add one day when creating from date string

In my javascript i want to convert date from date string.
i have string like
date = "Thu Sep 03 2015 19:30:00 GMT+0000"
Now i convert string using Date object.
var d = new Date(date);
But this gives me,
Fri Sep 04 2015 01:00:00 GMT+0530 (IST)
It automatically add one day into day. What is wrong?
It automatically add one day into day. What is wrong?
Nothing. The time you input is 19:30 GMT and the timezone on the device you're using is set to GMT+0530. Add 5 hours 30 minutes to 7:30pm and you get 01:00am the following day.
You should not use the Date constructor to parse strings, as it is inconsistent across browsers and until recently, entirely implementation dependent. Manually parse strings, or use a Date library.

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.

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