I used materialize datepicker to pick a date in french format. Now I need this date formatted back to a date object so I can use it in my api. Here's how I try to convert the date back to a normal format:
moment("dimanche 30 juillet 2017","dddd D MMMM YYYY").locale('fr').toDate();
But I receive Invalid Date. Is there a way to convert this date back using moment? Or can I somehow hook to materialize component to retrieve a normal date?
You need to set the fr locale before attempting to parse french day/monthnames.
moment.locale('fr');
moment("dimanche 30 juillet 2017","dddd D MMMM YYYY").toDate();
You can parse your input string passing locale parameter, see moment(String, String, String) docs:
As of version 2.0.0, a locale key can be passed as the third parameter to moment() and moment.utc().
Here a working sample:
var m = moment("dimanche 30 juillet 2017", "dddd D MMMM YYYY", 'fr');
console.log(m.toDate());
console.log(m.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment-with-locales.min.js"></script>
For further info see Changing locale globally and Changing locales locally.
Related
I'm trying to convert below-mentioned date to ISO format(MongoDB)
var d = { storedDate: '26/06/2020 05:55:29 PM' };
I'm however unable to find the parameter that I need to use to get it in the format which I want. I tried the below piece of code.
moment(d.storedDate).format("YYYY-MM-DD HH:mm Z");
How can I get it as ISODate("2020-06-26T17:55:29.274Z")
Please advice
moment(d.storedDate, 'DD/MM/YYYY HH:mm:ss').toISOString() will return ISO date only in UTC that you need for MongoDb. You need to provide the input date format as well.
If you want to store proper Date object, use
moment(d.storedDate, 'DD/MM/YYYY HH:mm:ss').toDate()
You should not store date/time values as strings, use proper data type.
I've been trying to display the local date and time after parsing ISO string to local timestamp based on navigator.language.
let sampleDate = new Date('2018-11-29T09:54:46.863207Z').toLocaleString(navigator.language);
So when I change the browser language preferences it reflects the date format in proper locale, but this doesn't seem to work with time part of the ISO string.
Result with browser locale en-AU--
Data Last Updated at 29/11/2018, 3:24:46 pm
Result with browser locale en-US -- Data Last Updated at 11/29/2018, 3:24:46 pm
See how only the date format changes based on the locale.It doesn't seem to affect the time component of the ISO 8601 string.
I've tried using moment.js to get the display the date and time in browser locale format but didn't find much success.
Am I missing something here?
It looks like you are trying to take a UTC date string, convert it to the client browser's local date and time, and format the date based on locale. You may be overthinking it a bit as JavaScript does most of this for you as long as you correctly create the Date object.
It is not recommended to parse date strings with the new Date() constructor, so the code example below uses a little regex and unpacking to parse the date string, then you can create the date in UTC with new Date(Date.UTC(...)). At that point, JavaScript will represent the date object in the client browser's local date and time automatically, then you can use toLocaleString() to apply formatting for the client browser's locale. For example:
const s = '2018-11-29T09:54:46.863207Z';
const [y, m, d, hh, mm, ss, ms] = s.match(/\d+/g);
const date = new Date(Date.UTC(y, m - 1, d, hh, mm, ss, ms));
const formatted = date.toLocaleString();
console.log(formatted);
If you need the Date for a particular time zone:
date.toLocaleDateString('en-AU', {timeZone: 'Australia/Sydney'})
date.toLocaleDateString('en-US', {timeZone: 'America/New_York'})
You can try this
console.log(
(new Date).toLocaleString('en-AU', {timeZone: 'Australia/Sydney'}) + "\n" +
(new Date).toLocaleString('en-US', {timeZone: 'America/New_York'})
)
18/10/2021, 10:05:25 pm
10/18/2021, 7:05:25 AM
I have a date string with this format
2016-08-12T15:22:43.698Z
how can I parse it to obtain a resulting string that looks like
Aug 12, 2016 5:22 PM
Is there libraries/component that could facilitate such operation or shall I do it manually by coping each part of the String?
var date = new moment('2016-08-12T15:22:43.698Z');
console.log(date.format('MMM DD, YYYY h:mm A'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.js"></script>
Use momentjs, and format the moment obj as your requirement.
If the string is in the ISO standard format, which it looks like it is, you can use Date.parse() or new Date() to turn the value into a Date object. With a Date, you can call toString() or toLocaleString() to get the date formatted in local time.
If you are targeting modern JavaScript environments, Intl.DateTimeFormat provides a very complete API for formatting the date in different locales.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat
I have a string in javascript as 2016-02-27 20:24:39 and I want to convert this as 27th Feb 08:24pm.
What is the easiest way to do in Javascript?
Checkout the JavaScript library called moment.js.
Since the default format for moment is ISO 8601 (YYYY-MM-DD HH:MM:SS), you don't need to tell moment how to parse the input String date (it defaults to ISO 8601), so you can simply write:
var now = "2016-02-27 20:24:39";
var formattedDate = moment(now).format("Do MMM HH:mma");
console.log(formattedDate);
Demo:
https://jsfiddle.net/gekd97dy/
More information about displaying in different formats can be read here:
http://momentjs.com/docs/#/displaying/
There is a non-standard Date method toLocaleFormat('%d-%b-%Y'). But appears to only work in Firefox for now.
Better use the date.format library (only 125 lines)
var date = new Date('2016-02-27 20:24:39');
dateFormat(date, "dS mmm, h:MMTT");
I have a the locale and a date in that locale format stored in a var in JavaScript
I need to convert that locale based date string to another local format
I have
locale : en-GB / en-US / es / ko
date : dd/mm/yyyy / mm/dd/yyyy yyyy.mm.dd
the above mentioned formats are not exact
ijust mean they are different for each
not i want that date to be displayed as "August 01 2013" like this
SO Finally i need a function(fromLocale,ToLocale,dateInFromLocaleFormat) which returns Date in ToLocale format
Can anyone help me on this
Can't you just use the JavaScript function dateFormat and set the format manually when you display the date?
for example: dateFormat("date", "mmmm dd yyyy");
more detailed here http://blog.stevenlevithan.com/archives/date-time-format