I have a date stored in variable like below
var date = moment(new Date()).valueOf();
I need to format it like below
CCYY-MM-DDThh:mm:ss[.sss]TZD
The Time Zone Definition is mandatory and MUST be either UTC (denoted by addition of the character 'Z' to the end of the string) or some offset from UTC (denoted by addition of '[+|-]' and 'hh:mm' to the end of the string).
I have tried like below
var required = moment.utc(date).format('CCYY-MM-DDThh:mm:ss[.sss]TZD')
But it is resulting like below
"CC14-06-03T07:59:15.sssT+00:003"
But the expected format examples are
UTC :
1969-07-21T02:56:15Z
Houston time :
1969-07-20T21:56:15-05:00
Just try with:
'YYYY-MM-DDThh:mm:ssZ'
Output:
2014-06-03T08:16:15+00:00
moment.js format documentation
Code:
new Date(moment('14/07/2022 14:27:50', 'DD/MM/YYYY HH:mm:ss').format('YYYY-MM-DDTHH:mm:ss'))
Output:
2022-07-14T17:27:50.000Z
Related
From below code:
const dateString = '1994-09-15T12:00:00-03:00';
const parsedDate = parseISO(dateString)
const dateFormat = 'MM-dd-yyyy HH:mm:ss xxx'
console.log(format(parsedDate, dateFormat, { }))
I expect:
09-15-1994 20:30:00 -03:00 But i get 09-15-1994 20:30:00 +05:30 as my local timezone is +05:30
What am i missing here?
The parseDate() creates a date based on GMT, e.g. is adjusted by 3h based on your input '1994-09-15T12:00:00-03:00'.
The format() function with xxx formats local time without the Z, such as -08:00, +05:30, +00:00. If you want to format with GMT string, specify OOOO, which will give you something like GMT-08:00, GMT+05:30, or GMT+00:00, which again has the offset adjusted based on your browser's time zone.
See Time-Zones docs below if you want to format to a time zone other than your browser's one, such as GMT-03:00.
Docs:
ISO 8601: https://en.wikipedia.org/wiki/ISO_8601
parseDate(): https://date-fns.org/v2.29.2/docs/parseISO
format(): https://date-fns.org/v2.29.2/docs/format
Time-Zones: https://date-fns.org/v2.29.2/docs/Time-Zones
Is there any way to convert any date string (not necessarily current date) (could be any format) to specific date format in Javascript. Like converting "MM-DD-YYYY" or "ddMMYYYY" to "DD-MMM-YYYY"?
I know that from current date as var date = new Date(), we can get time and hours but what to do in case of existing date string like "31/01/1999" to "31-JAN-1999".
Given the input date string can be of any format.
This is a common problem.
You should be able to do it with moment.js.
Ex.
moment("31/01/1999").formatWithJDF("dd - MM - yyyy");
Have a look here, https://momentjs.com/docs/ for more details.
Using DateFormatter.js
var date = new Date('2020-03-25 10:30:25');
var formatter = new DateFormatter();
displayFormat = 'D M d Y h:i:s';
var dateString = formatter.formatDate(date, displayFormat); // Wed Mar 25 2020 10:30:25
This can be done by first checking the input date format with the arrays or Map of regex provided, then need to convert that format to JS accepted format ISO format.
Once converted to ISO format this date now can be converted to any form with logic written for it in separate functions.
I have a date string of format dd.MM.yyyy HH:mm:ss but its UTC which I want to convert to local. How can I do it using moment library?
const dateStr = '20.09.2018 16:12:37';
const format = 'dd.MM.yyyy HH:mm:ss';
// for local time of UTC +3.00 new date Str will be 20.09.2018 19:12:37
You have to use moment.utc(String, String) to parse your string using UTC mode
By default, moment parses and displays in local time.
If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment()
Then you can use local() to convert it to local time:
Sets a flag on the original moment to use local time to display a moment instead of the original moment's time.
Please note that since you input dateStr is not in ISO 8601/RCF 2822 recognized format you have to specify format when parsing it. Morover moment tokens are case sensitive so you have to use uppercase YYYY instead of yyyy to parse years and uppercase DD to parse day of the month since lowercase dd stands for day of the week (Mon, Tue, etc).
Here a live sample:
const dateStr = '20.09.2018 16:12:37';
const format = 'DD.MM.YYYY HH:mm:ss';
console.log(moment.utc(dateStr, format).local().format(format));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
Have a look at Local vs UTC vs Offset guide to better understand how UTC and locale mode work in momentjs.
To convert from UTC to Local you need to use moment.local() method
const dateStr = '20.09.2018 16:12:37';
const format = 'DD.MM.YYYY HH:mm:ss';
var newDate = moment.utc().format(format);
var temp= moment.utc(newDate, format);
var local = moment(temp).local().format(format);
More info on the official Documentation
I am developing a Node.js application and I have to convert a German string date like am 13. Dezember 2017 to ISO Date and when I used moment.js library to convert it I got an invalid date, any solutions?
You can parse 13. Dezember 2017 using moment(String, String, String) and then use toISOString().
Since your input is neither in ISO 8601 recognized format, neither in RFC 2822 you have to provide format parameter. DD stands for day of the month, MMMM stands for month's name and YYYY stands for 4 digit year.
The third parameter tells moment to parse input using given locale:
As of version 2.0.0, a locale key can be passed as the third parameter to moment() and moment.utc().
Note that you have to import de locale to make it work (using moment-with-locales.js or /locales/de.js in browser or following Loading locales in NodeJS section for node).
Here a live example:
var m = moment('13. Dezember 2017', 'DD MMMM YYYY', 'de');
console.log( m.toISOString() );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment-with-locales.min.js"></script>
You can use the toISOString method, it return a Date object as a String, using the ISO standard:
var d = new Date();
var n = d.toISOString();
Have a look here:
https://momentjs.com/docs/
Do you need something like that:
moment('13. Dezember 2017', 'DD. MMMM YYYY', 'de').format(moment.ISO_8601);
You can easily format it using:
var now = moment(); // pass your date to moment
var formatedNow = moment().toISOString(now);
or
var formatedNow = now.toISOString();
I am having a problem with Moment.js.
Here are the codes.
var date = "2016-07-26 06:15 pm";
var unixDate = moment(date).unix();
var renderDate = moment.unix(unixDate).format("YYYY-MM-DD hh:mm a");
console.log(renderDate);
I want the value of "renderDate" to be same as the value of "date" variable. However, what gets printed out in the console is "2016-07-26 06:15 am".
I am completely lost as to why everything remains the same except "am/pm" at the end.
unix method accepts only number
You need to parse date before usage
moment('2016-07-28 06:15 PM', 'YYYY-MM-DD hh:mm a').format('YYYY/MM/DD hh:mm a')
Look at http://momentjs.com/docs/. You can only use the string without a format if it is in ISO 8601 format. In your example, it ignored the am/pm indicator and viewed the hour as being in 24-hour format.