I am trying to get date from string in specific format like:
const format = 'MM/dd/yyyy HH:mm';
const dt = "2021-03-11T22:00:00.000Z"; // expecting "03/11/2021 22:00"
console.log(moment(dt).format(format)); // but getting "03/Th/yyyy 23:00"
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
I tried js date, moment, luxon and I don't understand how to make this :(
I suspect that 000Z made problem but this is date I get.
The difference between Moment.js and Luxon is the case of the format keys. Please review the key/token tables.
Moment.js / String + Format
Luxon / Table of tokens
Note: Moment.js has been deprecated in favor of the team's newer library Luxon and other ECMA Script standards that are being introduced in ECMA-402. If you are considering using Moment.js now, just switch over to Luxon.
If you are using Luxon, you can call .toUTC() right before you format the date. Make sure your day of month (dd) and year (yyyy) format keys are lower-case.
const DateTime = luxon.DateTime;
const
format = 'MM/dd/yyyy HH:mm',
dt = "2021-03-11T22:00:00.000Z";
console.log(DateTime.fromISO(dt).toUTC().toFormat(format)); // 03/11/2021 22:00
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/1.26.0/luxon.min.js"></script>
If you are using Moment.js, you can call .utc() right before you format the date. Make sure your day of month (DD) and year (YYYY) format keys are upper-case.
const
format = 'MM/DD/YYYY HH:mm',
dt = "2021-03-11T22:00:00.000Z";
console.log(moment(dt).utc().format(format)); // 03/11/2021 22:00
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
Your format string is wrong, it should be: 'MM/DD/YYYY HH:mm'.
Then, if your date comes with ':000Z', you should remove it with the substring() method.
Working code:
const format = 'MM/DD/YYYY HH:mm';
const dateString = "2021-03-11T22:00:00:000Z";
const date = dateString.substring(0, dateString.length - 5);
console.log(moment(date).format(format)); // prints 03/11/2021 22:00
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
You need to use YYYY instead of yyyy and DD instead of dd to get the expected result.
By the way, I suggest using another library than Moment, like dayjs for instance.
Related
New to JavaScript dates and format and also using moment.js but I have the following date/time:
"2021-08-21 00:00:00"
but need to convert it to be this same date/time using moment.js, but in this format:
"2021-08-21T00:00:00.000Z"
To achieve the top date, I am using the following code:
let value = "00:00:00";
let cdt = moment(value, 'HH:mm:ss');
console.log(cdt.format('YYYY-MM-DD HH:mm:ss'))
Unsure what the .000Z are at the end but can someone pls assist with conversion but keeping to same date and time as first date/time
This is answered by How do I format a date as ISO 8601 in moment.js?
TLDR; you want moment().toISOString();
EDIT: Since you added your code, you'd probably want:
let value = "00:00:00";
let cdt = moment.utc(value, 'HH:mm:ss');
console.log(cdt.toISOString())
<script src="https://momentjs.com/downloads/moment.min.js"></script>
If I understand what you want ... no need for the no longer updated moment.js library
const dateString = "2021-08-21 00:00:00"
const asLocal = dateString.replace(" ", 'T');
const asUTC = asLocal + 'Z';
console.log(new Date(asLocal).toString());
console.log(new Date(asUTC).toUTCString());
I want to parse date through moment.js (which is normally really easy) but my date contains the abreviation of the timezone, and the symbol zz do not work here. (moment.js format)
const str1 = "2020-08-28 13:15 CST";
const date1 = moment(str1, "YYYY-MM-DD HH:mm zz");
console.log(date1);
<script src="https://momentjs.com/downloads/moment.min.js"></script>
<script src="https://momentjs.com/downloads/moment-with-locales.min.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone-with-data.js"></script>
As I am currently in UTC+2 timezone, it displays 2020-08-28T11:15:00.000Z which corresponds to 2020-08-28 13:15:00 UTC+2.
How could it recognize timezone abreviation ?
Use like this
moment.utc('2020-08-28 13:15 CST').format('YYYY-MM-DD HH:mm ZZ')
When I want to convert the Gregorian date to Persian date, it converts the value of the minute in the date conversion to error.
For example I want to convert this date time to Persian date:
2020-09-14T16:51:00+04:30 must convert to this 1399/06/24 16:51 but when convert date it show me this time 1399/06/24 00:06 it mistake to convert 16:51, show this: 00:06.
This is my code to convert date:
toPersianDate(date: any, format = 'YYYY/MM/DD HH:MM'): string {
let dateTime;
const MomentDate = moment(date, 'YYYY/MM/DD');
dateTime = MomentDate.locale('fa').format('jYYYY/jMM/jDD HH:jMM');
return dateTime;
}
What's the problem? How can I solve this problem?
the MM is used for month formatting, so it is trying to format the minutes into a month.
What you need to use is the small mm. Moreover, I don't this you need the j before the mm as the minutes are the same in Jalali time.
So what you actually need is this: MomentDate.locale('fa').format('jYYYY/jMM/jDD HH:mm');
You can read more about the formatting here.
I have a date with time and a timezone.
I need this formatted a specific way, which is why I am using moment.
Say the date string I want to put in is 2018-12-31 02:00:00.000 +00:00
I convert it into a specific format doing the following:
const properDate = moment(date, 'YYYY-MM-DD hh:mm:ss:ms')
However this is assuming the date is in UTC, and I want it to be in America/New York timezone, so the date and time I am expecting is actually:
2018-12-30 21:00:00.000
So my question is, how do I add in the timezone to moment when converting a date/time to a specific format?
I know it isn't as simple as passing a parameter. Is it another function I should call?
I have tried
const properDate = moment(date, 'YYYY-MM-DD hh:mm:ss:ms').tz('America/New_York')
but that didn't work either.
Edit: I should also mention that I am also using moment-timezone. It just seems that I can't get it to format the way I need it to as well as get it in the proper timezone.
So basically, I would think I could do something like:
const momentDate = moment(date)
const properDate = momentDate.tz(timezone).format('YYYY-MM-DD hh:mm:ss:ms')
Unfortunately as long as I try to format it, it won't work either.
Use moment.tz for parsing time string using a given timezone (e.g. 'America/New_York') while tz() is for converting between zones.
Here a live sample:
const date = '2018-12-30 21:00:00.000';
const properDate = moment.tz(date, 'YYYY-MM-DD hh:mm:ss.SSS', 'America/New_York')
console.log(properDate.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/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>
I'm using moment.js in my project, what I want is to convert a date in format dd-mm-yyyy to yyyy-mm-dd. I tried this but it didn't work, it gives me an invalid date:
alert(moment('14/10/2017').format('YYYY/MM/DD'));
You need to specify the current format of your date in moment():
var date = moment('14/10/2017', 'DD/MM/YYYY').format('YYYY/MM/DD');
console.log(date); // 2017/10/14
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>