Javascript ES6 Intl. Date/Time Formatting [duplicate] - javascript

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 3 years ago.
I'm trying to convert data using JS ES6 Intl.DateTimeFormat("pt-BR"), but everything i get is the previous day. Code i'm using:
var a = new Intl.DateTimeFormat("en-US");
var b = new Intl.DateTimeFormat("pt-BR");
console.log(a.format(new Date("2015-01-02"))); // "1/1/2015"
console.log(b.format(new Date("2015-01-02"))); // "01/01/2015"
Thank you in advance.

It is discouraged to rely on Date parsing a string. According to mdn:
Parsing of date strings with the Date constructor (and Date.parse(), which works the same way) is strongly discouraged due to browser differences and inconsistencies.
The format you use (yyyy-mm-dd) is interpreted as a UTC date (at midnight), so it does not correspond with your local date.
So better break down the string yourself into numerical arguments, and pass those to the Date constructor:
var a = new Intl.DateTimeFormat("en-US");
var b = new Intl.DateTimeFormat("pt-BR");
let date = new Date(..."2015-01-02".match(/\d+/g).map((d, i) => d-i%2));
console.log(a.format(date)); // "1/1/2015"
console.log(b.format(date)); // "01/01/2015"

The problem is related to timezone. The output is in your timezone, at 00:00 of the day 2nd of January UTC is 1st January 21:00 at your timezone, considering that your timezone is, for example, America/Sao_Paulo.
see: doc from mozilla
timeZone
The time zone to use. The only value implementations must recognize is "UTC"; the default is the runtime's default time zone. Implementations may also recognize the time zone names of the IANA time zone database, such as "Asia/Shanghai", "Asia/Kolkata", "America/New_York".

Related

How to convert zulu time to UTC +01:00 without moment [duplicate]

This question already has answers here:
How to ISO 8601 format a Date with Timezone Offset in JavaScript?
(21 answers)
Javascript date format like ISO but local
(12 answers)
Closed last year.
Hi i have a date that arrive with this format 2020-05-25T20:11:38Z, and i need to convert to 2020-05-25T21:11:38+01:00.
In my project is not installed moment.js is a big project, and the masters don't use it.
is there some where to make this change?
I have the timeZone for every zone.
I know that there is options like this getTimezoneOffset();
And i did find in stackoverflow, but i didn't find any response in javascript to change zulu to utc with offset.
Thanks for your indications
The format "2020-05-25T20:11:38Z" is a common standard ISO 8601 format, it is also produced by the default Date.prototype.toString method, however it's only with a UTC (+0) offset.
The above ISO 8601 format is reliably parsed by reasonably current built–in parsers (some very old implementations won't parse it correctly), so to get a Date object:
let date = new Date('2020-05-25T20:11:38Z');
Formatting it for a fixed +1 offset can done by adjusting the Date for the offset then formatting it as required by leveraging the default toISOString method, e.g.
// Initial timestamp
let s = '2020-05-25T20:11:38Z'
// Convert s to a Date
let d = new Date(s);
// Show that it's the same date
console.log(`Initial value: ${s}\n` +
`Parsed value : ${d.toISOString()}`);
// Create a new date with 1 hour added as 3,600,000 milliseconds
let e = new Date(d.getTime() + 3.6e6);
// Format and manually modify the offset part
let timestamp = e.toISOString().replace('Z','+01:00');
console.log(`Adjusted timestamp: ${timestamp}`);
// Parse back to date
console.log(`Parsed to a Date : ${new Date(timestamp).toISOString()}`);
The resulting timestamp can be parsed back to a Date that represents the same instant in time as the original string (last line).
Note that the adjusted Date is only created for the sake of formatting the timestamp, it shouldn't be used for anything else.
If, on the other hand, you want a general function to format dates as ISO 8601 with the local offset, there is likely an answer at Javascript date format like ISO but local that suits. If so, then this is a duplicate, e.g. this answer or this one.
Also, there are a number of libraries that will allow specifying the formatting and timezone as separate parameters, so consider using one if you're going to do a lot of date formatting or manipulation.

How to find the timeZoneOffset added in Date object [duplicate]

I have a string representing the current time: 2015-11-24T19:40:00. How do I parse this string in Javascript to get a Date represented by this string as the LOCAL TIME? Due to some restriction, I cannot use the library moment, but jquery is allowed. I know that someone has asked this question before, but the answer used moment
For example, if I run the script in California, then this string would represent 7PM pacific time, but if I run the script in NY then this string would represent Eastern Time?
I tried the following but Chrome and Firefox give me different results:
var str = "2015-11-24T19:40:00";
var date = new Date(str);
Chrome consumes it as UTC time (Tue Nov 24 2015 11:40:00 GMT-0800 (Pacific Standard Time)),
but Firefox consumes it as my local PACIFIC time (Tue Nov 24 2015 19:40:00 GMT-0800 (Pacific Standard Time))
I tried adding "Z" to str, like this var date = new Date(str+"Z");, then both browsers give me UTC time. Is there any similar letter to "Z" which tells all browsers (at least chrome, Firefox and Safari) to parse the string as local time zone?
Parsing of date strings using the Date constructor or Date.parse (which are essentially the same thing) is strongly recommended against.
If Date is called as a function and passed an ISO 8601 format date string without a timezone (such as 2015-11-24T19:40:00), you may get one of the following results:
Pre ES5 implementaitons may treat it as anything, even NaN (such as IE 8)
ES5 compliant implementations will treat it as UTC timezone
ECMAScript 2015 compliant implementations will treat it as local (which is consistent with ISO 8601)
A Date object has a time value which is UTC, and an offset based on system settings. When you send a Date to output, what you see is usually the result of Date.prototype.toString, which is an implementation dependent, human readable string representing the date and time, usually in a timezone based on system settings.
The best way to parse date strings is to do it manually. If you are assured that the format is consistent and valid, then parsing an ISO format string as a local date is as simple as:
/* #param {string} s - an ISO 8001 format date and time string
** with all components, e.g. 2015-11-24T19:40:00
** #returns {Date} - Date instance from parsing the string. May be NaN.
*/
function parseISOLocal(s) {
var b = s.split(/\D/);
return new Date(b[0], b[1]-1, b[2], b[3], b[4], b[5]);
}
document.write(parseISOLocal('2015-11-24T19:40:00'));
Note that parsing of ISO strings using Date.parse only accepts UTC, it does not accept any other timezone designation (noting the above behaviour if it's missing).
A variation on RobG's terrific answer.
Note that this will require that you run bleeding edge JavaScript as
it relies on the arrow notation and spread operator.
function parseDateISOString(s) {
let ds = s.split(/\D/).map(s => parseInt(s));
ds[1] = ds[1] - 1; // adjust month
return new Date(...ds);
}
Note that this doesn't take into account if the date/time given is in any timezone. It will assume local time. You can change new Date for Date.UTC to assume UTC.
There are technical reasons for why you would write you code like this. For example, here we apply the correct number of arguments, with their corresponding expected type. It's true that the Date constructor will turn strings into numbers but what could be happening is that there's a deoptimization taking place where the optimized code is expecting a number but sees a string and takes a slower path. Not a big deal but I try to write my JavaScript to avoid such things. We also won't be indexing outside the bounds of the array if less than 6 components can be found in the string which is also one of those things you can do in JavaScript but it has subtle deoptimization caveats.
Where Date is called as a constructor with more than one argument, the specified arguments represent local time.
I also have a much faster way than using the string.split() because we already know where the numbers are:
return new Date(Number(date.substring(0, 4)), Number(date.substring(5, 7))-1,
Number(date.substring(8, 10)), Number(date.substring(11, 13)),
Number(date.substring(14, 16)), Number(date.substring(17, 19)));
This will work with and/or without the 'T' and 'Z' strings and still has decent performance. I added the explicit Number conversion (faster and better than parseInt) so this also compiles in TypeScript.
Number

Date from input type="date"to new Date is incorrect date output [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 3 years ago.
I have an input="date" of which i am trying to pull the day, month, and year individually and it seems to be off by a day or so. For example if i Put in 01/01/1900 it seems to be spitting back out 1899 11 31. So for a basic example I have -
function clickDate() {
const dateinput = document.querySelector(".date").value;
const dateObj = new Date(dateinput);
console.log("pull date", dateObj.getFullYear(), dateObj.getMonth(), dateObj.getDate());
};
<input class="date" type="date">
<button onClick="clickDate()"> format</button>
you can see if you input 01/01/1900 the console outputs "pull date" 1899 11 31". Unsure what i am overlooking here.
Your issue lies with UTC versus local timezone offsets.
Using the built–in parser, a string in the format YYYY-MM-DD is interpreted as UTC, whereas other strings will be interpreted as local.
Use of the built–in parser is discouraged:
Note: Parsing of date strings with the Date constructor (and Date.parse(), which works the same way) is strongly discouraged due to browser differences and inconsistencies. Support for RFC 2822 format strings is by convention only. Support for ISO 8601 formats differs in that date-only strings (e.g. "1970-01-01") are treated as UTC, not local. (mdn)
On the other hand, getFullYear, getMonth, and getDate etc. return local values.
Here is a demonstration:
const dateInput = '2019-02-05'
const dateObj = new Date(dateInput); // parsed as UTC
console.log("pull date", dateObj.getUTCFullYear(), dateObj.getUTCMonth(), dateObj.getUTCDate()); // UTC
console.log("pull date", dateObj.getFullYear(), dateObj.getMonth(), dateObj.getDate()); // local
You have two choices:
1) The simplest being stick with the getUTC... functions.
2) If that doesn't work because you need to return the date object or manipulate it, then parse the input value and construct Date using individual year, month, day. This works because when using this constructor, the inputs are interpreted as local.
const dateInput = '2019-02-05';
let dateParts = dateInput.split('-');
dateParts[1]--; // month in Date constructor is 0-indexed (e.g. '02' represents March);
const dateObj = new Date(...dateParts); // parsed as local
console.log("pull date", dateObj.getUTCFullYear(), dateObj.getUTCMonth(), dateObj.getUTCDate()); // UTC
console.log("pull date", dateObj.getFullYear(), dateObj.getMonth(), dateObj.getDate()); // local

I have a UTC string and I want to convert it to UTC Date Object in JavaScript [duplicate]

This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
Closed 6 years ago.
I am using JavaScript, I have a JSON call that returns my UTC DateTimes as strings and I want to convert them to UTC Date objects. Has anyone done this? I do not want to go to local time at all.
On any vaguely-modern browser, you just pass that into Date with a "Z" on the end:
var dt = new Date(yourString + "Z");
That format was defined for JavaScript in the ES5 specification in December 2009. The "Z" is important to ensure that the string is interpreted as UTC, not local time¹.
Date objects keep track of a specific instant in time, which is irrespective of timezone. If you use the Date's UTC methods (getUTCFullYear, getUTCMonths, etc.), you'll access that instant in UTC. (If you use the non-UTC methods, you'll access it translated to local time.)
Example:
var str = "2016-11-22T17:14:00";
var dt = new Date(str + "Z");
console.log("UTC string:");
console.log(dt.toUTCString());
console.log("Local string");
console.log(dt.toString());
console.log("Hours UTC: " + dt.getUTCHours());
console.log("Hours local: " + dt.getHours());
¹ There's a bit of history in regard to that. When the format was originally added to the ES5 specification, it was supposed to be a subset of ISO-8601, but the ES5 spec said that no timezone indicator meant UTC, whereas in ISO-8601, no timezone indicator means local time. That lead to inconsistent implementations, where some were true to the ES5 specification, and others were true to ISO-8601. The bug was fixed in the ES2015 specification in June 2015. Now JavaScript's date/time format really is a subset of ISO-8601, except that JavaScript's Date always has a time component (all the way down to milliseconds), while ISO-8601 has the concept that a value may only be as specific as it is precise. That is, 2016-11-22 in JavaScript is a specific date and time, but in ISO-8601, it's just the date (no time is implied). Consequently, the current (ES2016) JavaScript specification says:
When the time zone offset is absent, date-only forms are interpreted as a UTC time and date-time forms are interpreted as a local time.
So 2016-11-22 is interpreted as 2016-11-22 at midnight UTC, but 2016-11-22T00:00:00 is interpreted as 2016-11-22 at midnight local time. Bizarre, but true. Of course, this latest language in the specification may not be correctly implemented by all implementations yet (I note that Chrome 54 gets it wrong, for instance).
Bottom line: You need that "Z" to ensure the string is parsed as UTC.

Unabe to parse date of different culture

I want to parse date
var newDateTime = new Date(Date.parse($("#SelctedCalendarDay").val()));
$("#SelctedCalendarDay").val() value is 14-okt-2014 in string.
When the date is 14-Oct-2014 it parses it correctly.
So how can I parse this date 14-okt-2014?
I'm not sure what language you are using (Dutch?) so I'll use English, it's easy to substitute whatever language you like. You can parse a date string like 14-Oct-2014 using:
function parseDMMMY(s) {
// Split on . - or / characters
var b = s.split(/[-.\/]/);
// Months in English, change to whatever language suits
var months = {jan:0, feb:1, mar:2, apr:3, may:4, jun:5,
jul:6, aug:7, sep:8, oct:9, nov:10, dec:11};
// Create a date object
return new Date(b[2], months[b[1].toLowerCase()], b[0]);
}
console.log(parseDMMMY('14-Oct-2014')); // Tue Oct 14 2014 00:00:00
Note that the above will create a local date object. It doesn't do any validation of values so if you give it a string like 31-Jun-2014 you'll get a Date object for 01-Jul-2014. It's easy to add validation (it takes one more line of code), but that may not be required if you know only valid strings will be passed to the function.
From the documentation of Date.parse # Mozilla
The date time string may be in ISO 8601 format. For example,
"2011-10-10" (just date) or "2011-10-10T14:48:00" (date and time) can
be passed and parsed. The UTC time zone is used to interpret arguments
in ISO 8601 format that do not contain time zone information (note
that ECMAScript ed 6 draft specifies that date time strings without a
time zone are to be treated as local, not UTC).
It seems to me the native Date.parse does not convert date strings matching a specific locale. There also seem to be some slight differences between specific browsers.
Maybe you want to use a library like moment.js for that?
Or you may want to wire up some PHP strtotime to your JS.

Categories