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')
Related
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.
I have a date in the format yyyy-MM-dd HH:mm:ss that I am using to create moments for x axes values in chart.js
I get invalid date when I am newing up an instance of the moment.js class
$.each(unique, function (index, value) {
var momentDate = moment(value, "DD MMM YY HH:mm:ss");
momentsArray.push(momentDate);
});
I have read the documentation and I believe I am following the right ISO format for creating moment.js object. What I am doing wrong ?
According to your date value "2020-10-19 15:50:20", your format string should be "YYYY-MM-DD HH:mm:ss"
I am getting translated UTC date from the backend and need to convert the date to localised local time format before displaying on the screen.
I have been using,
moment.locale(currentLocale); //Here currentLocale is the user's locale.
localtime = moment.utc(inputDate).toDate(); // inputDate = 2017年11月01日 AM10:42 (GMT)
moment(localtime).format(formatType); //formatType = "MMMM Do YYYY, h:mm A"
This works fine when the date is in english but not when we get translated date.
Is there any way we can process translated date to convert it to locale date and time and if possible translate digits too.
You should give the parser the format of the string you're parsing. You must be ignoring the error you're getting.
var inputDate = 'inputDate = 2017年11月01日 AM10:42 (GMT)'
var parseFormat = 'YYYY MM DD Ah:mm';
var printFormat = 'MMMM Do YYYY, h:mm A';
var d = moment.utc(inputDate, parseFormat).toDate();
console.log('Local: ' + moment(d).format(printFormat));
console.log('UTC : ' + moment.utc(d).format(printFormat));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.min.js"></script>
The technique of parsing a string to a moment object, then creating an ECMAScript Date just to parse it back into a moment.utc object is weird to me. There doesn't seem to be a way of parsing a string as UTC without then setting the output offset to UTC too.
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>
I have date in this format
2017-01-09T18:30:00.000Z
I am using moment.js and I am trying to convert it into DD-MMM-YYY HH:mm:ss --> 09-Jan-2017 18:30:00
I have tried this method
dateTime = moment("2017-01-09T18:30:00.000Z").format("DD-MMM-YYYY HH:mm:ss");
But I got output like this 9/1/2017 0:00
What I miss?
The reason you're getting 00:00 is because moment converts the date object to your timezone. In order to remove this and format the date without the timezone, use moment.utc().
Update your fiddle to this:
var dateTime = moment.utc("2017-01-09T18:30:00.000Z").format("DD-MMM-YYYY HH:mm:ss");
document.getElementById('output').innerText = dateTime;
and it will work, outputting: 09-Jan-2017 18:30:00
Try using:
dateTime = moment("2017-01-09T18:30:00.000Z").format("DD-MMM-YYYY HH:mm:ss"); // note the extra Y in YYYY
console.log(dateTime);
// => 10-Jan-2017 05:30:00
For the most part it looks like what you are doing is right.