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?
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.
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.
The JavaScript exception "invalid date" occurs when a string leading to an invalid date has been provided to Date or Date. parse() .
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.
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.
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/
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With