Invalid date when parsing with locale it












2














I need to parse a date in the "it" locale with momentjs, and I'm doing this



import moment from 'moment';
import 'moment/locale/it';

moment.locale("it");
let d = "20/12/2018"; // 20 dec 2018
let mm = moment(d);
console.log(mm.format("DD MM YYYY"));


What I get is "invalid date" and I don't understand why. Can you help me?



Using the "en" locale (with the date written as 12/20/2018) all is ok










share|improve this question




















  • 4




    You need to tell Moment what format you're feeding it. let mm = moment(d, "DD/MM/YYYY");
    – blex
    Dec 27 '18 at 22:14










  • And if I don't know the format? The format depends on the locale, and I don't know what locale the user will be using
    – cdarwin
    Dec 27 '18 at 22:16








  • 2




    Is this part of a form to be filled by a user? If so, I don't know of any website that does not tell the user Please enter the date in this format: ... or uses a calendar widget so that the user does not even have to think about it. Edit: I'm not judging, I'm just asking
    – blex
    Dec 27 '18 at 22:18








  • 1




    do not rely over their locale for storing the date, but format it using their locale when you are displaying it. You can instead store it in a single way (standard format, ms, etc...)
    – quirimmo
    Dec 27 '18 at 22:23








  • 1




    Some browsers allow setting a language, I don't know any that allow setting different regional settings to the host system. The point is that date formatting for input and output in general is not necessarily dependent on those settings. For example, The Daily Mail, a British newspaper, writes the date on the front page as "Thursday, December 27, 2018". The Times on its web site uses "27 December 2018". Both are very British.
    – RobG
    Dec 27 '18 at 22:38


















2














I need to parse a date in the "it" locale with momentjs, and I'm doing this



import moment from 'moment';
import 'moment/locale/it';

moment.locale("it");
let d = "20/12/2018"; // 20 dec 2018
let mm = moment(d);
console.log(mm.format("DD MM YYYY"));


What I get is "invalid date" and I don't understand why. Can you help me?



Using the "en" locale (with the date written as 12/20/2018) all is ok










share|improve this question




















  • 4




    You need to tell Moment what format you're feeding it. let mm = moment(d, "DD/MM/YYYY");
    – blex
    Dec 27 '18 at 22:14










  • And if I don't know the format? The format depends on the locale, and I don't know what locale the user will be using
    – cdarwin
    Dec 27 '18 at 22:16








  • 2




    Is this part of a form to be filled by a user? If so, I don't know of any website that does not tell the user Please enter the date in this format: ... or uses a calendar widget so that the user does not even have to think about it. Edit: I'm not judging, I'm just asking
    – blex
    Dec 27 '18 at 22:18








  • 1




    do not rely over their locale for storing the date, but format it using their locale when you are displaying it. You can instead store it in a single way (standard format, ms, etc...)
    – quirimmo
    Dec 27 '18 at 22:23








  • 1




    Some browsers allow setting a language, I don't know any that allow setting different regional settings to the host system. The point is that date formatting for input and output in general is not necessarily dependent on those settings. For example, The Daily Mail, a British newspaper, writes the date on the front page as "Thursday, December 27, 2018". The Times on its web site uses "27 December 2018". Both are very British.
    – RobG
    Dec 27 '18 at 22:38
















2












2








2







I need to parse a date in the "it" locale with momentjs, and I'm doing this



import moment from 'moment';
import 'moment/locale/it';

moment.locale("it");
let d = "20/12/2018"; // 20 dec 2018
let mm = moment(d);
console.log(mm.format("DD MM YYYY"));


What I get is "invalid date" and I don't understand why. Can you help me?



Using the "en" locale (with the date written as 12/20/2018) all is ok










share|improve this question















I need to parse a date in the "it" locale with momentjs, and I'm doing this



import moment from 'moment';
import 'moment/locale/it';

moment.locale("it");
let d = "20/12/2018"; // 20 dec 2018
let mm = moment(d);
console.log(mm.format("DD MM YYYY"));


What I get is "invalid date" and I don't understand why. Can you help me?



Using the "en" locale (with the date written as 12/20/2018) all is ok







javascript date momentjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 27 '18 at 22:09









Federico Grandi

2,70021127




2,70021127










asked Dec 27 '18 at 22:06









cdarwin

1,45372952




1,45372952








  • 4




    You need to tell Moment what format you're feeding it. let mm = moment(d, "DD/MM/YYYY");
    – blex
    Dec 27 '18 at 22:14










  • And if I don't know the format? The format depends on the locale, and I don't know what locale the user will be using
    – cdarwin
    Dec 27 '18 at 22:16








  • 2




    Is this part of a form to be filled by a user? If so, I don't know of any website that does not tell the user Please enter the date in this format: ... or uses a calendar widget so that the user does not even have to think about it. Edit: I'm not judging, I'm just asking
    – blex
    Dec 27 '18 at 22:18








  • 1




    do not rely over their locale for storing the date, but format it using their locale when you are displaying it. You can instead store it in a single way (standard format, ms, etc...)
    – quirimmo
    Dec 27 '18 at 22:23








  • 1




    Some browsers allow setting a language, I don't know any that allow setting different regional settings to the host system. The point is that date formatting for input and output in general is not necessarily dependent on those settings. For example, The Daily Mail, a British newspaper, writes the date on the front page as "Thursday, December 27, 2018". The Times on its web site uses "27 December 2018". Both are very British.
    – RobG
    Dec 27 '18 at 22:38
















  • 4




    You need to tell Moment what format you're feeding it. let mm = moment(d, "DD/MM/YYYY");
    – blex
    Dec 27 '18 at 22:14










  • And if I don't know the format? The format depends on the locale, and I don't know what locale the user will be using
    – cdarwin
    Dec 27 '18 at 22:16








  • 2




    Is this part of a form to be filled by a user? If so, I don't know of any website that does not tell the user Please enter the date in this format: ... or uses a calendar widget so that the user does not even have to think about it. Edit: I'm not judging, I'm just asking
    – blex
    Dec 27 '18 at 22:18








  • 1




    do not rely over their locale for storing the date, but format it using their locale when you are displaying it. You can instead store it in a single way (standard format, ms, etc...)
    – quirimmo
    Dec 27 '18 at 22:23








  • 1




    Some browsers allow setting a language, I don't know any that allow setting different regional settings to the host system. The point is that date formatting for input and output in general is not necessarily dependent on those settings. For example, The Daily Mail, a British newspaper, writes the date on the front page as "Thursday, December 27, 2018". The Times on its web site uses "27 December 2018". Both are very British.
    – RobG
    Dec 27 '18 at 22:38










4




4




You need to tell Moment what format you're feeding it. let mm = moment(d, "DD/MM/YYYY");
– blex
Dec 27 '18 at 22:14




You need to tell Moment what format you're feeding it. let mm = moment(d, "DD/MM/YYYY");
– blex
Dec 27 '18 at 22:14












And if I don't know the format? The format depends on the locale, and I don't know what locale the user will be using
– cdarwin
Dec 27 '18 at 22:16






And if I don't know the format? The format depends on the locale, and I don't know what locale the user will be using
– cdarwin
Dec 27 '18 at 22:16






2




2




Is this part of a form to be filled by a user? If so, I don't know of any website that does not tell the user Please enter the date in this format: ... or uses a calendar widget so that the user does not even have to think about it. Edit: I'm not judging, I'm just asking
– blex
Dec 27 '18 at 22:18






Is this part of a form to be filled by a user? If so, I don't know of any website that does not tell the user Please enter the date in this format: ... or uses a calendar widget so that the user does not even have to think about it. Edit: I'm not judging, I'm just asking
– blex
Dec 27 '18 at 22:18






1




1




do not rely over their locale for storing the date, but format it using their locale when you are displaying it. You can instead store it in a single way (standard format, ms, etc...)
– quirimmo
Dec 27 '18 at 22:23






do not rely over their locale for storing the date, but format it using their locale when you are displaying it. You can instead store it in a single way (standard format, ms, etc...)
– quirimmo
Dec 27 '18 at 22:23






1




1




Some browsers allow setting a language, I don't know any that allow setting different regional settings to the host system. The point is that date formatting for input and output in general is not necessarily dependent on those settings. For example, The Daily Mail, a British newspaper, writes the date on the front page as "Thursday, December 27, 2018". The Times on its web site uses "27 December 2018". Both are very British.
– RobG
Dec 27 '18 at 22:38






Some browsers allow setting a language, I don't know any that allow setting different regional settings to the host system. The point is that date formatting for input and output in general is not necessarily dependent on those settings. For example, The Daily Mail, a British newspaper, writes the date on the front page as "Thursday, December 27, 2018". The Times on its web site uses "27 December 2018". Both are very British.
– RobG
Dec 27 '18 at 22:38














1 Answer
1






active

oldest

votes


















2














The below snippet will accomplish what you want. It takes moment's date format for a given local and passes it to the constructor when creating a moment.



With that said, the comments above raise a lot of good points and this is not a reliable way to be handling dates.



For example, if someone in Italy entered a date string in the en MM/DD/YYYY format this would break



let localeFormat = moment.localeData('it').longDateFormat('L');
console.log(localeFormat) // DD/MM/YYYY

let d = "20/12/2018"; // 20 dec 2018
let mm = moment(d, localeFormat);
console.log(mm.format("DD MM YYYY"));





share|improve this answer























  • this is what I thought Moment could do
    – cdarwin
    Dec 27 '18 at 22:42











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53951327%2finvalid-date-when-parsing-with-locale-it%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














The below snippet will accomplish what you want. It takes moment's date format for a given local and passes it to the constructor when creating a moment.



With that said, the comments above raise a lot of good points and this is not a reliable way to be handling dates.



For example, if someone in Italy entered a date string in the en MM/DD/YYYY format this would break



let localeFormat = moment.localeData('it').longDateFormat('L');
console.log(localeFormat) // DD/MM/YYYY

let d = "20/12/2018"; // 20 dec 2018
let mm = moment(d, localeFormat);
console.log(mm.format("DD MM YYYY"));





share|improve this answer























  • this is what I thought Moment could do
    – cdarwin
    Dec 27 '18 at 22:42
















2














The below snippet will accomplish what you want. It takes moment's date format for a given local and passes it to the constructor when creating a moment.



With that said, the comments above raise a lot of good points and this is not a reliable way to be handling dates.



For example, if someone in Italy entered a date string in the en MM/DD/YYYY format this would break



let localeFormat = moment.localeData('it').longDateFormat('L');
console.log(localeFormat) // DD/MM/YYYY

let d = "20/12/2018"; // 20 dec 2018
let mm = moment(d, localeFormat);
console.log(mm.format("DD MM YYYY"));





share|improve this answer























  • this is what I thought Moment could do
    – cdarwin
    Dec 27 '18 at 22:42














2












2








2






The below snippet will accomplish what you want. It takes moment's date format for a given local and passes it to the constructor when creating a moment.



With that said, the comments above raise a lot of good points and this is not a reliable way to be handling dates.



For example, if someone in Italy entered a date string in the en MM/DD/YYYY format this would break



let localeFormat = moment.localeData('it').longDateFormat('L');
console.log(localeFormat) // DD/MM/YYYY

let d = "20/12/2018"; // 20 dec 2018
let mm = moment(d, localeFormat);
console.log(mm.format("DD MM YYYY"));





share|improve this answer














The below snippet will accomplish what you want. It takes moment's date format for a given local and passes it to the constructor when creating a moment.



With that said, the comments above raise a lot of good points and this is not a reliable way to be handling dates.



For example, if someone in Italy entered a date string in the en MM/DD/YYYY format this would break



let localeFormat = moment.localeData('it').longDateFormat('L');
console.log(localeFormat) // DD/MM/YYYY

let d = "20/12/2018"; // 20 dec 2018
let mm = moment(d, localeFormat);
console.log(mm.format("DD MM YYYY"));






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 27 '18 at 22:42

























answered Dec 27 '18 at 22:36









Mike Gillett

536413




536413












  • this is what I thought Moment could do
    – cdarwin
    Dec 27 '18 at 22:42


















  • this is what I thought Moment could do
    – cdarwin
    Dec 27 '18 at 22:42
















this is what I thought Moment could do
– cdarwin
Dec 27 '18 at 22:42




this is what I thought Moment could do
– cdarwin
Dec 27 '18 at 22:42


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53951327%2finvalid-date-when-parsing-with-locale-it%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas