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

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>

Related

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

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.

Adding moment locale break the initial date

I have a weird issue with moment that i cannot explain:
I have my date : Wed Feb 28 2018 16:24:37 GMT+0100 (CET)
When I add import 'moment/locale/fr'; the same date becomes Sun Jan 28 2018 16:24:37 GMT+0100 (CET)
Can anyone explain what am I doing wrong ?
here is the complete example
import React from 'react';
import moment from 'moment';
// ====== adding locale break the date ======= //
// import 'moment/locale/fr';
// time value is : Wed Feb 28 2018 16:24:37 GMT+0100 (CET)
const Time = ({ time }) => (
<p>
{moment(time).format('DD-MM-YYYY HH:mm:ss')}
</p>
);
// output without locale/fr is : 28-02-2018 16:24:37
// output with locale/fr is : 28-01-2018 16:24:37
After that I will use .fromNow()moment function to display the time distance between now and the given time.
If you parse a string without providing the input format, moment.js first tries ISO 8601 format. If it doesn't match, it falls back to the built-in parser. You should be getting a warning not to do that (because it's a bad idea).
If you provide the parse format ('ddd MMM DD YYYY HH:mm:ss ZZ') with a locale file of French and don't specify that the input string is English, then the string seems to be parsed as if it was French and "Feb" is parsed as "janvier" (January) rather than février (February).
You need to provide the parse format and language of the input string:
moment('Wed Feb 28 2018 16:24:37 GMT+0100 (CET)', 'ddd MMM DD YYYY HH:mm:ss ZZ', 'en')
// Without providing parse format
console.log(moment('Wed Feb 28 2018 16:24:37 GMT+0100 (CET)').format('DD-MM-YYYY HH:mm:ss'));
// With parse format but not input langauge
console.log(moment('Wed Feb 28 2018 16:24:37 GMT+0100 (CET)', 'ddd MMM DD YYYY HH:mm:ss ZZ').format('DD-MM-YYYY HH:mm:ss'));
// With parse format and input langauge (recommended approach)
console.log(moment('Wed Feb 28 2018 16:24:37 GMT+0100 (CET)', 'ddd MMM DD YYYY HH:mm:ss ZZ', 'en').format('DD-MM-YYYY HH:mm:ss'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/locale/fr.js"></script>
Try something like this (we need import from moment/locale/fr to make sure it loads on client side). So parse in English, then switch locale to the one you need, and then output the time difference with fromNow:
import moment from 'moment';
import momentFr from 'moment/locale/fr';
const Time = ({ time }) => (
<p>
{moment(time, 'ddd MMM DD YYYY HH:mm:ss Z').locale('fr').fromNow()}
</p>
);

Parse Javascript date to C# DateTime, FF data is other then Chrome date

I convert javascript date to C# DateTime.
When I use firefox, JavaScript return date to my C# function:
string jsDate = "Fri Dec 05 2014 00:00:00 GMT+0100";
so, I parse it to C# DateTime using:
DateTime.TryParseExact(JsDate, "ddd MMM dd yyyy HH:mm:ss 'GMT'K", CultureInfo.InvariantCulture, DateTimeStyles.None, out Date)
When I using Chrome, js return me date in format: "Fri Dec 05 2014 00:00:00 GMT+0100 (Środkowoeuropejski czas stand.)" (
Central European standard time)
How I can parse the second one time?
Basically, you shouldn't use the default string representation from the browser. Otherwise you need to know which language it's going to use for month names etc, and you're basically fighting a losing battle.
I would strongly recommend that you format the string in a culture-neutral way when you pass it from the browser to the server - e.g. as ISO-8601 such as yyyy-MM-ddTHH:mm:ssZ. You should consider whether or not you need the time zone offset - you may just want to send it in UTC for simplicity. (If you do send the offset from UTC, you should probably parse it as a DateTimeOffset in your C# code.)
String dateString = "Fri Dec 05 2014 00:00:00 GMT+0100 (Środkowoeuropejski czas stand.)"
dateString = dateString.subStr(0,dateString.indexOf('(')-1);
DateTime.TryParseExact(dateString, "ddd MMM dd yyyy HH:mm:ss 'GMT'K", CultureInfo.InvariantCulture, DateTimeStyles.None, out Date)

Categories

Resources