Showing the GMT time with momentjs - javascript

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>

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"

Date-fns is returning incorrect dates

I have a collection of date strings stored in a database in this format:
2018-06-28T14:06:26.524Z
2018-07-02T10:32:18.818Z
2018-07-06T15:08:50.233Z
I need to convert these dates into a format like this on the frontend:
28 June 2018 14:06:26
02 July 2018 10:32:18
06 July 2018 08:50:23
My attempt at doing this using date-fns is:
format(new Date(date), 'dd MMMM yyyy HH:MM:ss')
The problem is that the dates returned from the above are incorrect:
2018-06-28T14:06:26.524Z returns 28 June 2018 15:06:26
2018-07-02T10:32:18.818Z returns 02 July 2018 11:07:18
2018-07-06T15:08:50.233Z returns 06 July 2018 16:07:50
What am I doing wrong here and how to I fix this so the dates are returned correctly?
You messed up the time:
HH:mm:ss not HH:MM:ss
MM is for month and mm is for minute - docs
You misspelled HH:MM:ss should be HH:mm:ss look at this list for an explanation on all formats: Date formats
Example:
dd MMMM yyyy HH:mm:ss: 22 August 2006 06:30: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>

"Invalid date" parsing date with moment.js

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>

Angular Date Filter not showing UTC ..! [duplicate]

i am using angularjs filter date. i have used this one
var date = new Date("09 apr 2015 12:00:50 UT");
$filter('date')(date, "dd MMM yyyy hh:mm:ss a Z");
Output:
09 Apr 2015 08:19:04 PM +5:30
i need output:
09 Apr 2015 08:19:04 PM GMT +5:30 (IST)
i don't think you can do it out of the box with plain angular.js, but you can use a library like angular-moment.
moment.js supports variety of formats: http://momentjs.com/docs/#/displaying/format/

Categories

Resources