Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moment.js "Invalid date"

I just tried to make a text input with formatted date. It is working fine on Google Chrome, but when I run it on Mozilla Firefox (26.0) it says: Invalid date.

You can see and run my example in both browsers and hopefully understand the problem.

JS Code:

$( "#h_reg_date" ).datepicker();
$("#h_reg_date").datepicker("option", "dateFormat", "yy-M-dd");

$("#save_data").on("click", function(){
 var reg_date = $("#h_reg_date").val();
  console.log(reg_date);
  reg_date =  moment(reg_date).format("YYYY/MM/DD");
  console.log(reg_date);
  return;
});

HTML:

<input type="text" id="h_reg_date"class="abs" style="top:135px; left:150px;" size="10" readonly >
<input type="button" id="save_data" class="abs" style="top:370px; left:-200px;" value="Add Data">

Is there any solution to this problem?

like image 962
Sameera Silva Avatar asked Feb 04 '14 01:02

Sameera Silva


People also ask

Why is moment invalid date?

You cannot just throw any date format into it and expect it to magically recognize the format. Moment. js relies on the date parsing functionality of JavaScript if you do not specify and other format.

How can I check if a date is invalid in moment?

isValid() is the method available on moment which tells if the date is valid or not. MomentJS also provides many parsing flags which can be used to check for date validation.

Why does JavaScript show invalid date?

The JavaScript exception "invalid date" occurs when a string leading to an invalid date has been provided to Date or Date. parse() .

How can I set a valid empty date in MomentJS?

If you use the moment adapter and moment dates, you should set your component's date variable to null , and not to moment(null) . Internally, the datepicker will ask the adapter if the date is valid but moment(null). isValid() returns false thus triggering the parse validator that will return an error.


3 Answers

Suppose your date is in format 'DD-MM-YYYY' & you want to display it in DD-MMM format then you can do this by first telling moment function which format your date currently is & then asking it to convert it to required format. Below is the example:

var taskDueDate = moment(dueDate, 'DD-MM-YYYY').format('DD-MMM'); 

This way you can convert your date in any format.

like image 78
Sachin T Sawant Avatar answered Oct 02 '22 12:10

Sachin T Sawant


Change the format of the datepicker to:

$("#h_reg_date").datepicker("option", "dateFormat", "yy/mm/dd"); 

JS Fiddle: http://jsfiddle.net/MMm86/1/

To put the date in your requested format use:

$( "#h_reg_date" ).datepicker(); $("#h_reg_date").datepicker("option", "dateFormat", "yy/M/dd");  $("#save_data").on("click", function(){  var reg_date = $("#h_reg_date").val();   console.log(reg_date);   reg_date =  moment(reg_date).format("YYYY/MMM/DD");   console.log(reg_date);   return; }); 

JS Fiddle: http://jsfiddle.net/MMm86/2/

like image 41
Kevin Bowersox Avatar answered Oct 02 '22 10:10

Kevin Bowersox


It seems to be a known issue, per their docs. http://momentjs.com/docs/#/parsing/string/

Looks like they suggest passing in the time string and the format.

like image 40
thescientist Avatar answered Oct 02 '22 11:10

thescientist