I would like to know how to parse date strings that could have different date format.
For now I do the following to parse my date strings:
var parseDate = d3.time.format("%Y-%m-%d %H:%M").parse;
How to specify multiple date format to my parseDate function?
new Date('YOUR DATE STRING')
Also checkout moment.js,
You can use moment('YOUR DATE STRING')
moment will try to interpret the String according to standard formats, if not it will fall back to Javascript's native Date() constructor.
This should work for standard ISO format or a variety of commonly seen date formats. Obviously it is not magic and will not understand all possible permutations of YYYY MM and DD
Related
let date="20-05-2022";
const dates=new Date(date);
const out=dates.toString();
console.log(out);
The above snippet output is invalid date.
How to I convert this kind of date format to string.
How to I convert that??.
The JavaScript Date constructor only supports the parsing of strings that meet a simplified version of the ISO 8601 datetime format.
You have a number of options, including:
Rearrange the string to be 'YYYY-MM-DD' before passing it to the Date constructor, or
Parse the year, month and day number values and then pass these into the Date constructor as individual parameters like so: new Date(year, monthIndex, day), or
Use a library like moment.js.
Be careful about timezones: date-only strings (e.g. "1970-01-01") are treated as UTC, while date-time strings (e.g. "1970-01-01T12:00") are treated as local, or, if a timezone offset is supplied (e.g. Z or +02:00), according to that.
And when passing individual numbers for each datetime component to the Date constructor, values are treated as local.
A new, more featureful time API (Temporal) is under development.
I have the string "18/04/19 5:17 PM EDT" which represents a date.
I'm using moment and the add-on moment-timezone and I need to convert this sting into a timestamp.
I'm trying something as:
var date = moment("18/04/19 5:17 PM EDT").format('DD/MM/YY h:m a z');
alert(date);
But this is not working and saying "invalid date".
Please note that moment(String):
When creating a moment from a string, we first check if the string matches known ISO 8601 formats, we then check if the string matches the RFC 2822 Date time format before dropping to the fall back of new Date(string) if a known format is not found.
Warning: Browser support for parsing strings is inconsistent. Because there is no specification on which formats should be supported, what works in some browsers will not work in other browsers.
For consistent results parsing anything other than ISO 8601 strings, you should use String + Format.
so you are getting Invalid Date because your input is neither in ISO 8601 nor RFC 2822 recognized format, then you have to provide format parameter when parsing it.
moment(String, String) does not accept 'z' token, so you have to use moment-timezone to parse your input using zone, see Parsing in Zone docs:
The moment.tz constructor takes all the same arguments as the moment constructor, but uses the last argument as a time zone identifier.
You can use format() and other methods listed in the Displaying section of the docs (e.g. valueOf()) to display the value of a moment object.
Here a live sample:
var date = moment.tz("18/04/19 5:17 PM EDT", 'DD/MM/YY h:m A', 'America/New_York');
console.log(date.valueOf()); // 1555622220000
console.log(date.format()); // 2019-04-18T17:17:00-04:00
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data-2012-2022.min.js"></script>
As a side note, remeber that time zone abbreviations are ambiguous, see here for additional info.
I am using moment.js to compare two dates in my javascript code.
The input values are passed from a JSON string.
When I provide date 21/01/2050 00:00 (MM/DD/YYYY HH:mm FORMAT), the resulting date shows as 21/1/1950 00:00.
I tried using the below options:
moment(item["End Date"]).utc().format()
moment(item["Start Date"]).format('MM/DD/YYYY HH:mm')
moment(new Date(item["Start Date"])).format('MM/DD/YYYY HH:mm')
But these are still giving me year as 1950 instead of 2050.
Is there a way to format this one correctly?
Since your input is not in ISO 8601 or RFC 2822 format recognized by moment(String)
When creating a moment from a string, we first check if the string matches known ISO 8601 formats, we then check if the string matches the RFC 2822 Date time format before dropping to the fall back of new Date(string) if a known format is not found.
Warning: Browser support for parsing strings is inconsistent. Because there is no specification on which formats should be supported, what works in some browsers will not work in other browsers.
For consistent results parsing anything other than ISO 8601 strings, you should use String + Format.
So you have to use moment(String, String), passing 'DD/MM/YYYY HH:mm' as second argument.
Here a live sample:
var input = "21/01/2050 00:00";
console.log( moment(input, 'DD/MM/YYYY HH:mm').format('MM/DD/YYYY HH:mm') );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
I need the localized date format pattern string i.e. 'dd mmmm yyyy' or 'mmmm dd, yyyy'.
I need to pass this string to a external plugin that uses it for showing dates.
Note: I do not need to format a date. I need to get the format "pattern string" of the user's locale.
It does not seem like it's possible to do this natively in JavaScript - you could either import a library like Moment.js or you could use a static map from a resource such as this one.
Same date in different format returning different values. Hence comparison is not matching.
Example below:
alert(new Date(Date.parse('2015-02-03')));
alert(new Date(Date.parse('02/03/2015')));
or
alert(new Date('2015-02-03').setHours(0,0,0,0));
alert(new Date('02/03/2015').setHours(0,0,0,0));
I am looking for a way to match both exactly. Can anyone help?
With the ISO 8601 specification YYYY-MM-DD format without a timezone given, UTC is assumed. With a non-ISO format like YYYY/MM/DD or MM/DD/YYYY or DD/MM/YYYY, the result is implementation (e.g., browser) specific.