Used ng prime datepicker in application.
Getting date in Fri May 14 2021 00:00:00 GMT+0530 (India Standard Time) format as response from api.
Then converting Fri May 14 2021 00:00:00 GMT+0530 (India Standard Time) this response with new Date().
Then binding the response to ng prime datepiker.
Working fine in chrome.
In firefox it is showing "Invalid date".
See the documentation for the Date constructor:
A string value representing a date, specified in a format recognized by the Date.parse() method. (These formats are IETF-compliant RFC 2822 timestamps, and also strings in a version of ISO8601.)
Since you aren't passing one of those formats, you are dependent on implementations having non-standard support for your format.
Chrome does. Firefox doesn't.
Use a date parsing library that lets you specify the format (such as date-fns/parse) or change the API so it outputs dates in the standard format.
You can as well, use the dateStringToDate function in date-fran module. It converts date formats of the form "Fri May 14 2021" to actual date instances.
Related
I am using new Date() to create a date object for a specific date. But it returns two different dates in different timezones. See below:
When my machine is in Indian Timezone (IST)
console.log(new Date('2020-08-28')); //returns Fri Aug 28 2020 05:30:00 GMT+0530 (India Standard Time)
When my machine is in US timezone (CST)
console.log(new Date('2020-08-28')); //returns Fri Aug 27 2020 19:00:00 GMT-0500 (Central Daylight Time)
How is this happening even when I am telling the JS to create a date for a given string? The string here is '2020-08-28'.
Why is this happening and how to ignore the timezone in this case?
From the ECMAScript specification:
When the UTC offset representation is absent, date-only forms are interpreted as a UTC time and date-time forms are interpreted as a local time.
The forms themselves are described in the Date Time String Format.
It is important to note that treating date-only forms as UTC is in conflict with the ISO 8601 spec which treats such forms as local time. This is a known issue and was done deliberately in the name of "web reality".
When you instantiate a Date by passing a string, it's supposed to be a full ISO 8601 string which specifies the time zone. As you dont specify it, it takes Greenwich Mean Time (±00:00), then uses your local timezone when displaying it. Use this if you want to display the date as GMT:
console.log(new Date('2020-08-28').toUTCString());
On the console in chrome browser of windows and Ubuntu I entered Date() object and I found both have a different way to write the timezone for that date.
Ubuntu:
"Thu Aug 10 2017 18:45:18 GMT+0530 (IST)"
Windows:
"Thu Aug 10 2017 18:45:18 GMT+0530 (India Standard Time)"
According to my use case the output on Ubuntu is correct, the time zone IST is what expected. But on windows it always shows up India Standard Time.
Is there a way to fix this for windows.
According to my use case the output on Ubuntu is correct
Neither of them is correct. Or incorrect. The string form of a Date from toString (which is what those appear to be) is almost completely unspecified. The only requirement is that it represent the date, and that Date() and Date.parse should be able to parse it.
If you want something reliable cross browser*, use toISOString.
* Obsolete browsers may not have toISOString. Even only vaguely-modern browsers do.
My computer is with US region settings and date formats and -8h GMT timezone, but i am not exactly in US region. Why when getting the date through new Date i can see that the timezone offset is correct, but when i try and convert it to localedateString i get my really local date string?
var date = new Date();
///here date is (for example) Mon Jan 15 2001 12:00:00 GMT-0800 (Pacific Standard Time)
var localeString = date.toLocateDateString();
///or date.toLocaleString()
/// localeString here comes in my real locale date format 15.01.2001 г or 15.01.2001 г., 12:00:00 when using toLocaleString()
The output of toLocaleString is entirely implementation dependent and is inconsistent across browsers currently in use. It does not necessarily respect system settings (e.g. Chrome does not respect my system settings for the order of the components) and there is no requirement in ECMA-262 for it to do so.
For reliable results, format the output manually in an unambiguous format, e.g. 25 Feb 2017. A library can help, but if you only need one format then a simple function will suffice.
The implementation of Date.parse in Chrome has very unexpected behavior.
For example, Date.parse('foo 2014') should ideally return NaN as it is not a proper date format. But in Chrome this returns the value 1388514600000, which is equivalent to the date "Wed Jan 01 2014 00:00:00 GMT+0530 (India Standard Time)".
As long as the string ends with some sort of year, a proper date value is returned. This will not let us to properly validate dates.
According to ECMAScript this behavior is implementation dependent and chrome does a very loose validation and some how converts to a date.
Is there anyway in chrome to properly validate date without using any library or using heavy logic and matching patterns?
You can test date string with regular expression before parse.
I want to convert date string to Date by javascript, use this code:
var date = new Date('2013-02-27T17:00:00');
alert(date);
'2013-02-27T17:00:00' is UTC time in JSON object from server.
But the result of above code is different between Firefox and Chrome:
Firefox returns:
Wed Feb 27 2013 17:00:00 GMT+0700 (SE Asia Standard Time)
Chrome returns:
Thu Feb 28 2013 00:00:00 GMT+0700 (SE Asia Standard Time)
It's different 1 day, the correct result I would expect is the result from Chrome.
Demo code: http://jsfiddle.net/xHtqa/2/
How can I fix this problem to get the same result from both?
The correct format for UTC would be 2013-02-27T17:00:00Z (Z is for Zulu Time). Append Z if not present to get correct UTC datetime string.
Yeah, unfortunately the date-parsing algorithms are implementation-dependent. From the specification of Date.parse (which is used by new Date):
The String may be interpreted as a local time, a UTC time, or a time in some other time zone, depending on the contents of the String. The function first attempts to parse the format of the String according to the rules called out in Date Time String Format (15.9.1.15). If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats.
To make the Date constructor not (maybe) use the local timezone, use a datetime string with timezone information, e.g. "2013-02-27T17:00:00Z". However, it is hard to find a format that is reliable parsed by every browser - the ISO format is not recognised by IE<8 (see JavaScript: Which browsers support parsing of ISO-8601 Date String with Date.parse). Better, use a unix timestamp, i.e. milliseconds since unix epoch, or use a regular expression to break the string down in its parts and then feed those into Date.UTC.
I found one thing here. It seems the native Firefox Inspector Console might have a bug:
If I run "new Date()" in the native Inspector, it shows a date with wrong timezone, GMT locale, but running the same command in the Firebug Extension Console, the date shown uses my correct timezone (GMT-3:00).
Noticed that FireFox wasn't returning the same result as Chrome. Looks like the format you use in kendo.toString for date makes a difference.
The last console result is what I needed:
Try using moment.js. It goes very well and in similar fashion with all the browsers. comes with many formatting options. use moment('date').format("") instead of New Date('date')