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>
);
Related
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>
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.
moment('Sat Sep 12 2015 15:00:00 GMT+0100 (GMT Daylight Time)').format()
// Error: core-test.js:52920 Uncaught Error: input not handled by moment(…)
I am miffed as to why moment cannot handle this date, even when stripping it down I still can't get it to work it out.
var date = 'Sat Sep 12 2015 15:00:00 GMT+0100 (GMT Daylight Time)'.split(' GMT')[0];
moment(date).format()
//The same error
You can provide moment a format to use to parse your string: http://momentjs.com/docs/#/parsing/string-format/
But your date format isn't possible with the options available so you have to strip out some of the information. Namely the reference the timezone name for the offset "GMT" and the "GMT Daylight Time". Since the timezone is encoded in the "+0100" part I'm going to assume that it's fine to remove those references.
First create a function to "clean" the string date:
function cleanDateString(formattedDate) {
return formattedDate.replace(/(.*?)(\w{3})(((\+|-)\d{4}).*)/g,"$1$4");
}
If you invoke the function on the string you provided cleanDateString('Sat Sep 12 2015 15:00:00 GMT+0100 (GMT Daylight Time)') the output will be
"Sat Sep 12 2015 15:00:00 +0100"
Now this is something that can be parsed by moment using the right format.
If you look at http://momentjs.com/docs/#/displaying/format/ then the format that you would need for this string is
'ddd MMM DD YYYY mm:hh:ss ZZ'
If you combine the two things from above, then you can get your date like this
function parseCustomDate(formattedDate) {
return moment(cleanDateString(formattedDate),'ddd MMM DD YYYY mm:hh:ss ZZ');
}
And that will work with your given string
parseCustomDate('Sat Sep 12 2015 15:00:00 GMT+0100 (GMT Daylight Time)')
Note on the Regex
If you want details on how exactly the regex works, you can look at the "Explanation" and the "Match Information" sections at this link: https://regex101.com/r/tH6hM9/1. I used that to tweak the groupings and the rules
Your Date Format should look like this:
var date2 = '2015-09-12T15:00:00+00:00'; // UTC
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)
I've got an input date as the following:
Thu May 17 2012 18:00:00 GMT+0100 (BST)
However, with the following :
var dateString = 'hu May 17 2012 18:00:00 GMT+0100 (BST)';
document.write($.format.date(dateString, "ddd MMMM dd, HH:mm"));
The resulting output is 1 hour faster than I'd expect:
Thursday May 17, 18:00
Seems to be ignoring the GMT+0100 (BST) part...
Is there anyway to get it to display as 17:00?
You will have to convert the timezone explicitly. You can create a separate function for it.
Please check this link :
http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/6016329