Parsing date from DD/MM/YYYY with Luxon javascript - javascript

I have a date in this format "DD/MM/YYYY" and I want to convert it to a DateTime object with Luxon library. How can I do it?
I know we can use methods like .fromISO(), .fromHTTP(), .fromSQL(), .fromJSDate(), and .fromFormat() and none of them accepts the format I have, for example: "31/12/2022"
I was trying with fromFormat( date, 'D' ) but it's invalid because "D" format is equal to "MM/DD/YYYY".

You can use fromFormat :
Create a DateTime from an input string and format string. Defaults to en-US if no locale has been specified, regardless of the system's locale. For a table of tokens and their interpretations, see here.
passing "d/M/yyyy" as second argument. Example:
const DateTime = luxon.DateTime;
console.log(DateTime.fromFormat("31/12/2022", "d/M/yyyy").toISO());
<script src="https://cdn.jsdelivr.net/npm/luxon#3.1.1/build/global/luxon.min.js"></script>
Please have a look at Parsing section of the docs and Table of tokens to see the list of available tokens. As docs states:
Note that many tokens supported by the formatter are not supported by the parser.
in your case "D" is a format token you can use standalone tokens to parse your input string.

Related

Parse string to Date in JavaScript with any format but specific locale for ambiguous cases

Is there a JavaScript library that can parse strings to datetime as in e.g. C#? What I mean is a way such that I don't have to specify each date format that could occur (e.g. 1/19/2021, 2021-1-1, 1-Jan-21), but I can specify locale so that in ambiguous cases where multiple options are available right option would be chosen?
E.g. in C# I can do something like this:
CultureInfo culture = new CultureInfo("en-GB");
DateTime myDate = DateTime.Pare(myDateString, culture, DateTimeStyles.None);
This will know that 1/2/2021 is February 1st (and not January 2nd), but it will also properly parse 1/19/2021 (it will not throw exception because 19th month doesn't exist) and everything else.
You could use this from Luxon library:
public static fromFormat(text: string, fmt: string, opts: Object): DateTime.
Create a DateTime from an input string and format string. Defaults to en-US if no locale has been specified, regardless of the system's locale.
See docs here: https://moment.github.io/luxon/docs/class/src/datetime.js~DateTime.html

converting dates into different format gives invalid date error in Angular

I have a date which is like this "19/05/2020". I am trying to convert it into "yyyy-MM-dd" format using pipe. So the date should be look like "2020-05-19".
this._datePipe.transform("19/05/2020", 'yyyy-MM-dd');
It is giving me an error saying Unable to convert "19/04/2020" into a date' for pipe 'DatePipe
Also tried converting the string to date but again invalid date message is getting display.
Is there any way so that we can convert this date that is "19/05/2020" to a valid one so that we can perform datepipe on it.
I guess you need to convert your date having a supported formatted string :
For example, "2011-10-10" (date-only form), "2011-10-10T14:48:00" (date-time form), or "2011-10-10T14:48:00.000+09:00" (date-time form with milliseconds and time zone) can be passed and will be parsed
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
This is valid for Date.parse() or new Date().
So you'll need to parse your string with another method.
This may helps you :
How to convert dd/mm/yyyy string into JavaScript Date object?

Localized short time string using moment.js

How do I get time string from moment.js object with respect to current locale?
moment().format('HH:mm')
always get the same result regardless of localization.
I want similar result as I would use in angular using shortTime:
formatDate(new Date(), 'shortTime'): // HH:mm, resp hh:mm a
You can simply use Localized formats listed in format() docs
Because preferred formatting differs based on locale, there are a few tokens that can be used to format a moment based on its locale.
There are upper and lower case variations on the same formats. The lowercase version is intended to be the shortened version of its uppercase counterpart.
Here a live sample:
console.log( moment().format('LT') );
moment.locale('it');
console.log( moment().format('LT') );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment-with-locales.min.js"></script>

Javascript String to date conversion without date format but passing locale

I have this user input date field, where user can enter date as string such '010117' or '01012017' which I am converting to US date standard format(MM/dd/yyyy) using JavaScript. If we have to go international we want user to input date as string as per their country standard.
Question is, Is there any library for JavaScript that we can use to pass the date as string and locale and it will convert the string to their locale standard date format.
We don't want to pass date format for each country as it will be too much of a task to do that for each country.
Thanks in advance.
There is JavaScript library MomentJS which provides very good features about date and time.
We can pass the string to MomentJS and it will give date.
By default MomentJS comes with English locale. You can change the locale by passing locale string to below function:
moment.locale(myLocale);
To get user's locale, you can do the following:
var myLocale = window.navigator.userLanguage || window.navigator.language;
Refer to the the link:
https://momentjs.com/docs/#/i18n/

Parsing icalendar datetime YYYYMMDDTHHMMSSZ with moment.js

I'm trying to parse the YYYYMMDDTHHMMSSZ format with moment.js, but I always get "Invalide date".
Is there a way to parse it?
moment takes a second parameter that indicates the format, so you can do:
var dtg = '20140112T121537Z';
moment(dtg, 'YYYYMMDDTHHmmssZ');

Categories