Momentjs is saying a non valid date is valid - momentjs

We have an address, that according to momentjs is reporting as valid.. any thoughts?
You can see the behavior in this fiddle
var wrong_date = "7840 W HICKS STREET";
document.write(moment(wrong_date, "YYYY-MM-DD HH:mm:ss").isValid()); //returns true
https://jsfiddle.net/jeffbeagley/2c9urj5v/1/

Use strick format parsing:
var wrong_date = "7840 W HICKS STREET";
document.write(moment(wrong_date, "YYYY-MM-DD HH:mm:ss", true).isValid());
Notice the third boolean parameter set to true.
This is a strick match on the format.

Moment's parser is very forgiving, and this can lead to undesired behavior. As of version 2.3.0, you may specify a boolean for the last argument to make Moment use strict parsing. Strict parsing requires that the format and input match exactly, including delimeters.
document.write(moment(wrong_date, "YYYY-MM-DD HH:mm:ss").isValid()); //returns true
document.write(moment(wrong_date, "YYYY-MM-DD HH:mm:ss", true).isValid()); //returns false
You can use both locale and strictness.
moment('2012-10-14', 'YYYY-MM-DD', 'fr', true);

Related

How to check if a string denotes a 24 hour valid time format using momentJS?

The code:-
moment(moment("2022-12-12").format("YYYY-MM-DD"), "YYYY-MM-DD", true).isValid()
returns true as 2022-12-12 is a valid date in YYYY-MM-DD format.
Using the same logic, I tried to check if var timeString = "16:00:00" is a valid time or not.
However, the following code:-
moment(moment("16:00:00").format("HH:mm:ss"), "HH:mm:ss", true).isValid()
always gives me false.
What am I doing wrong?
It's always giving true as you are formatting to that particular type first than checking if its valid which is why its always true.
I think you want something like this.
function isTimeFormat(time) {
return moment(time, 'HH:mm:ss', true).isValid();
}

moment.js will not parse UK format date even when setting the locale

Quite simply, this is my code:
http://jsfiddle.net/NibblyPig/k9zb4ysp/
moment.locale('en-GB');
var d = moment('22/12/2019');
alert(d);
I would expect this to parse, however it says invalid date.
I have referenced moment.js and the locale/en-gb.js
I'm writing a global control so the date may come in in a variety of formats.
If I put in a variety of American dates they all work, for example 12/12/2019, 12/12/2019 23:04 etc.
However the locale command does not appear to do anything and I cannot get a single date to parse. What am I doing wrong?
You need to pass the format as the second argument for moment(), as discussed here:
moment.locale('en-GB');
var d = moment('22/12/2019', 'DD/MM/YYYY');
alert(d);
https://jsfiddle.net/a4gu6kfz/
From the docs:
If you know the format of an input string, you can use that to parse a
moment.
moment("12-25-1995", "MM-DD-YYYY");
I think that there is no need to write your own complex logic to parse your input, you can use moment(String, String) (or moment(String, String[], String, Boolean)), as suggested by Thales Minussi's answer.
moment(String) is the good choice only if your input is in ISO 8601 or RFC 2822 compliant form.
In your case, you can probably use Localized formats listed in the format section of the docs. If you have a list of possible formats, I think that the best choice is tho use moment(String, String[]).
Please note that, by default: Moment's parser is very forgiving, so using default Forgiving Mode will handle "any" character as separator.
Here a live sample:
moment.locale('en-GB');
['22/12/2019', '22/12/2019 15:00',
'22-12-2019', '22-12-2019 15:00',
'1-3-2019', '1-12-2019', '22-1-2019'
].forEach((elem) => {
var d = moment(elem, 'L LT');
console.log(d.format());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/locale/en-gb.js"></script>
Still hoping there's a nice moment js way to do this but in the meantime I just bashed this together. Pretty nasty and it will probably go wrong in 80 years or so.
http://jsfiddle.net/NibblyPig/k9zb4ysp/22/
var a = "23/03/19 12:42:21.123";
var datePart = a.substring(0, a.indexOf(" "));
var timePart = a.substring(a.indexOf(" ") + 1);
var dateParts = datePart.split("/");
if (dateParts[0].length == 1) dateParts[0] = "0" + dateParts[0];
if (dateParts[1].length == 1) dateParts[1] = "0" + dateParts[1];
if (dateParts[2].length == 2) {
var threshold = parseInt(new Date().getFullYear().toString().substring(2)) + 10;
if (parseFloat(dateParts[2]) > threshold ) {
dateParts[2] = "19" + dateParts[2];
}
else
{
dateParts[2] = "20" + dateParts[2];
}
}
alert (parseFloat(dateParts[2] + dateParts[1] + dateParts[0] + timePart.replace(/:/g, "").replace(/\./g, "")));
This won't solve every usecase, but in your specific example if you want just a simple date (with no time component) auto-parsed in UK format you can just use the 'L' format string having set the locale to 'en-GB'
Your example with this change (your jsfiddle also)
moment.locale('en-GB');
// just pass 'L' i.e. local date format as a parsing format here
var d = moment('22/12/2019', 'L');
alert(d);
It's quite nice because you get the auto parsing of various formats you wanted for free. For instance this works just the same:
var d = moment('22-12-2019', 'L');
You can return a date using moment.js in a desired format -
return moment(aDateVar).format('DD/MM/YYYY');

MomentJS can't convert a date with format YYYY-MM-DD HH:mm:ss.fff Z

I am trying to convert the next date using MomentJS:
const moment = require('moment');
var datetime = "2017-11-19 02:45:22.011 +00:00";
var newDate = moment(datetime);
But it fails and the next message appears:
Deprecation warning: value provided is not in a recognized RFC2822 or
ISO format. moment construction falls back to js Date(), which is not
reliable across all browsers and versions. Non RFC2822/ISO date formats
are discouraged and will be removed in an upcoming major release.
Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more
info.
Snippet showing the issue:
var datetime = "2017-11-19 02:45:22.011 +00:00";
var newDate = moment(datetime);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.2/moment.min.js"></script>
I also tried using:
moment.utc(datetime);
but failed.
As moment(String) docs says:
When creating a moment from a string, we first check if the string matches known ISO 8601 formats, we then check if the string matches the RFC 2822 Date time format before dropping to the fall back of new Date(string) if a known format is not found.
2017-11-19 02:45:22.011 +00:00 is not in ISO 8601 compliant format because there is a space between fractional seconds and UTC offset (2017-11-19 02:45:22.011+00:00 is an ISO 8601 version of your input). So you can use moment(String, String), here a live sample:
var datetime = "2017-11-19 02:45:22.011 +00:00";
var newDate = moment(datetime, 'YYYY-MM-DDTHH:mm:ss.fff Z');
console.log(newDate.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.2/moment.min.js"></script>
Reformatting the date string from:
2017-11-19 02:45:22.011 +00:00
To:
2017-11-19T02:45:22.011Z
Solved it

Why moment isValid() API does not support format 'L' for non 'en' local

Moment API isValid seems only support format 'L' for local is 'en':
locale set to 'en':
moment(new Date()).locale('en').format('L') //"04/27/2016"
moment('04/27/2016', 'L', true).isValid() //true
local set to 'zh-cn':
moment(new Date()).locale('zh-cn').format('L') //"2016-04-27"
moment('2016-04-27', 'L', true).isValid() //false, why here is false?
As i don't want to hard code 'MM-DD-YYYY' for 'zh-cn' here, is there any other good way?
Setting the locale like that only affects the current instance of moment, not any future instances. Future instances use the default locale again, which in your case means 'L' is not the YYYY-MM-DD format.
You can either set the locale globally:
moment.locale('zh-cn');
moment(new Date()).format('L') // 2016-04-27
moment('2016-04-27', 'L', true).isValid() // true
Or specify the locale for each instance, including the one you use for parsing:
moment(new Date()).locale('zh-cn').format('L') // 2016-04-27
moment('2016-04-27', 'L', 'zh-cn', true).isValid() // true

Format exception String was not recognized as a valid DateTime

objTour.tourStartDate =
Convert.ToDateTime(
DateTime.ParseExact(txtTourStartDate.Text, "dd/MM/yyyy", null)
.ToString("MM/dd/yyyy"));
where txtTourStartDate.Text="16/08/2012".
I have searched and read all posts related to this.
In a custom date format string, / denotes the culture-specific date separator, not the literal character /. Thus, the result of your code depends on the user's (or the server's) localization settings.
To make your code independent of culture-specific settings, you have two options:
Explicitly specify a culture that uses a slash as the date separator, e.g.
DateTime.ParseExact(txtTourStartDate.Text, "dd/MM/yyyy",
CultureInfo.InvariantCulture)
or escape the character, e.g.
DateTime.ParseExact(txtTourStartDate.Text, #"dd\/MM\/yyyy", null)
(note the # and the \).
Both should yield the desired result.
This will be enough:
objTour.tourStartDate = DateTime.ParseExact(txtTourStartDate.Text,
"dd/MM/yyyy",
CultureInfo.InvariantCulture);
Your original code works, although you are doing lot of unnecessary conversions. (DateTime -> ToString -> ToDateTime), the real issue is InvariantCulture. Since you are passing null for CultureInfo try CultureInfo.InvariantCulture.
Your original code:
objTour.tourStartDate =
Convert.ToDateTime(
DateTime.ParseExact(txtTourStartDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture)
.ToString("MM/dd/yyyy"));
A better one could be:
objTour.tourStartDate =
DateTime.ParseExact(txtTourStartDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture)

Resources