Moment.js "Invalid date"

Sameera Silva picture Sameera Silva · Feb 4, 2014 · Viewed 75k times · Source

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?

Answer

Sachin T Sawant picture Sachin T Sawant · Mar 8, 2016

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.