"Invalid date" parsing date with moment.js - javascript

I'm trying to calculate the relative date from a given one like the following
Mon Nov 21 11:48:33 CET 2016
I think the given date follows this pattern:
EEE MMM d HH:mm:ss zzz yyyy
So, I'm trying to get the relative date with this line:
moment("Mon Nov 21 11:48:33 CET 2016", "EEE MMM d HH:mm:ss zzz yyyy").fromNow();
But I'm receiving an "Invalid date"...
I've changed the "EEE" for "ddd" following some advices but then I'm getting a bad relative date.
moment("Mon Nov 21 12:30:40 CET 2016", "ddd MMM d HH:mm:ss zzz yyyy").fromNow() > 20 days ago
Any idea about what I'm doing wrong?
Thank you so much!

Assuming you're using moment-timezone in addition to Moment, there are a few problems:
zzz is invalid undocumented; it's just z. (It seems to be allowed, though.)
EEE is invalid. E is for day numbers. You wanted ddd (day name). See the documentation.
You've also used d (day name) where you wanted D or DD (day of month number). Again, see the docs linked above.
This works with moment+moment-timezone:
moment("Mon Nov 21 11:48:33 CET 2016", "ddd MMM D HH:mm:ss z yyyy").fromNow();
// -------------------------------------^^^-----^----------^
Example:
console.log(moment("Mon Nov 21 11:48:33 CET 2016", "ddd MMM D HH:mm:ss z yyyy").fromNow());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.16.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.9/moment-timezone-with-data-2010-2020.min.js"></script>
However, I don't think Moment supports parsing that z, because I can't find anything in the Moment Timezone docs saying it enhances parsing, and when I run the above, it acts as though the date/time is in my timezone (GMT), not CET; and in fact I can swap in any timezone indicator I want (EST, PST, etc.) and it still treats the string as my local time.

For Day of the week use ddd
Check http://momentjs.com/docs/ for more information on formats.
console.log(moment("Mon Nov 21 11:48:33 CET 2016", "ddd MMM d HH:mm:ss zzz yyyy").fromNow());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.16.0/moment.min.js"></script>

Related

How do I parse with both Day of Week and Month in date-fns?

I have a string Mon Mar 28 00:00:00 EDT 2022 which I want to parse but it's not working
dateFns.parse(theDate, "eee MMM d HH:mm:ss X yyyy", null)
Getting
RangeError: The format string mustn't contain eee and MMM at the same time
Well, you can try this:
dateFns.parse(theDate, "EEE MMM d HH:mm:ss X yyyy", new Date())
but this still wrong, the "EDT" string cause error, I've tried with "-04" instead, it works. So maybe you should try some way convert the "EDT, UTC,.." string to timezone format "-04" "+07"

convert "Thu Sep 19 14:24:59 UTC 2019" to moment date

I try to convert this date:
created_at= "Thu Sep 19 14:24:59 UTC 2019"
using this:
let elementDate=moment(created_at)
but I am getting:
moment.invalid(/* Fri Aug 30 09:52:04 UTC 2019 */)
I also tried this:
moment(created_at,"DDD Mo DD hh:mm:ss UTC YYYY")
but seems that it is not correct. Any thoughts?
From the Moment.js docs:
Unless you specify a time zone offset, parsing a string will create a
date in the current time zone.
If the date strings that you need to parse are all UTC, then you can simply use moment.utc() and fix your day of the week and month format tokens. Otherwise, you will have to do some additional pre-processing on your strings as it can't parse timezone abbreviations.
const dt = moment.utc('Thu Sep 19 14:24:59 UTC 2019', 'ddd MMM D HH:mm:ss [UTC] YYYY');
console.log(dt.format());
// 2019-09-19T14:24:59Z
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Showing the GMT time with momentjs

I started using momentjs a while ago. I was using before Date in order to covert epochtime to a nice format. Example:
return new Date(1000 * parseInt(timestamp));
Output:
Sun Apr 14 2019 21:23:38 GMT+0300 (Sarajevo Daylight Time)
I would like to get the following format:
Apr 14 2019 21:23:38 GMT+0300
So I tried to use momentjs. The format I used is: "MMM DD, YYYY HH:mm:ss" but it returns without GMT+0300. What format should I add?
For now what I did is:
moment.unix(timestamp).format("MMM DD, YYYY HH:mm:ss")
Output: Apr 14 2019 21:23:38.
Tried to read the docs but I could not find information about it.
Use ZZ to get offset and use "MMM DD YYYY HH:mm:ss [GMT]ZZ" to get the desired format.
console.log(moment.unix(1555424726).format("MMM DD YYYY HH:mm:ss [GMT]ZZ"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>

Moment.js Timezone not converting unless I pass in date as striing

I am having trouble with converting a datetime to the proper timezone
I do not understand why this is functioning like this.
d = "Thu Apr 26 2018 21:09:11 GMT-0700 (Pacific Daylight Time)"
moment.tz(d.toString(), this._timezone).format('MM/DD/YYYY h a')
returns 04/26/2018 3 pm
moment.tz(d, this._timezone).format('MM/DD/YYYY h a')
returns 04/26/2018 10 pm
Also moment.isMoment(d) returns false
also if I convert d to an ISO string before adjusting the TZ the TZ doesn't adjust
var d = "Thu Apr 26 2018 21:09:11 GMT-0700 (Pacific Daylight Time)";
console.log(moment.tz(d.toString(), 'America/Chicago').format('MM/DD/YYYY h a'));
console.log(moment.tz(d, 'America/Chicago').format('MM/DD/YYYY h a'));
console.log(moment.isMoment(d));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment-with-locales.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.16/moment-timezone-with-data.js"></script>
Automatic detection of non-ISO strings has been deprecated. See here for more information. The salient point is:
This deprecation warning is thrown when no known format is found for a
date passed into the string constructor. To work around this issue,
specify a format for the string being passed to moment().
So if you want to reliably parse the given string, you'll need to specify the format at parse-time as follows:
moment.tz(d, '<format here>', this._timezone);
I'm not sure exactly how to format your whole date correctly, but something like this should work:
var DATE_FORMAT = 'ddd MMM DD YYYY HH:mm:ss [GMT]Z'
var d = "Thu Apr 26 2018 21:09:11 GMT-0700 (Pacific Daylight Time)";
var DATE_FORMAT = "ddd MMM DD YYYY HH:mm:ss [GMT]Z"
console.log(moment.tz(d.toString(), DATE_FORMAT, 'America/Chicago').format('MM/DD/YYYY h a'));
console.log(moment.tz(d, DATE_FORMAT, 'America/Chicago').format('MM/DD/YYYY h a'));
console.log(moment.isMoment(d));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment-with-locales.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.16/moment-timezone-with-data.js"></script>
#Rajits information is correct but it was not the cause of the "odd" behavior.
although Thu Apr 26 2018 21:09:11 GMT-0700 (Pacific Daylight Time)" looks like a string it was actually an instance of a javascript date object.
new Date() was called on a UTC timestamp which resulted in that "string" therefore when passing the date instance directly to moment, moment could handle it but when turning it into a string with toString() it was changed into just a plain string.
Also calling new Date() on a UTC timestamp it automatically convert this timestamp into the browsers (clients computer) local timezone. So if you want to change your timestamp to an arbitrary timezone make sure not to call new Date() before.

How to customize year format in a date format using moment.js?

I need to get the following format using moment.js
Thu, 10 Oct 16, 2:00PM
The closest format I found using moment.js is using the following function.
moment(dateVar).format('llll')
where dataVar is the required date object.The output looks as follows
Thu, Oct 16, 2016 12:00 AM
I could not find any way to customise this date format or get the required result.
According to the moment docs you should be able to get your result by passing the following formatting string:
moment(dateVar).format('ddd, DD MMM YY, H:mmA');
I think this string should get you the desired result.
If I run it with the current date it returns this:
moment(new Date()).format('ddd, DD MMM YY, H:mmA');
//"Wed, 02 Nov 16, 10:25AM"
And extensive list of your display options with formatting strings is here:
http://momentjs.com/docs/#/displaying/
See http://momentjs.com/docs/#/displaying/
What you need is
ddd, D MMM YY, h:mmA
ddd - Day of week
D -Day of month
etc

Categories

Resources