Dd-mm-yyyy Date Format With Moment.js
Would like to get date format like 12-September-2017 after adding months with moment.js. I'm using datepicker for date fields. current output is Th-10-yyyy. Also, getting warning
Solution 1:
You have to use:
var formatedportalexpdate = portalexpdate.format('DD-MMMM-YYYY');
as stated in the format
docs.
Moment tokens are case sensitive, dd
stands as day of the week, while DD
stands for day of the month, use MMMM
to get full month name and YYYY
to get the 4 digit year.
Use moment(String, String)
instead of moment(input_value_inputstartdate)
to avoid Deprecation warning while parsing input_value_inputstartdate
, in your case you can do something like:
var inputstartdate = moment(input_value_inputstartdate, 'DD-MMMM-YYYY');
The full code could be like the following:
$(document).on("change", "#inputmonthadded", function (evt) {
var input_value_inputstartdate = $('#inputstartdate').val();
var input_value_inputmonthpurchased = $('#inputmonthpurchased').val();
var input_value_inputmonthadded = $('#inputmonthadded').val();
if (typeof input_value_inputstartdate != 'undefined' && input_value_inputstartdate) {
var inputstartdate = moment(input_value_inputstartdate, 'DD-MMMM-YYYY');
var portalexpdate = inputstartdate.add(input_value_inputmonthadded, 'months');
var formatedportalexpdate = portalexpdate.format('DD-MMMM-YYYY');
$('#inputportalexpirydate').val(formatedportalexpdate);
}
else {
$('#inputportalexpirydate').val('');
}
});
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script><inputtype="number"id="inputmonthadded"><inputtype="text"id="inputstartdate"value="12-September-2017"readonly><inputtype="text"id="inputportalexpirydate"readonly><inputtype="text"id="inputmonthpurchased">
Solution 2:
You need to follow moment's formatting style: https://momentjs.com/docs/#/displaying/
Therefore: format("D-MMMM-YYYY")
Note: using DD
will append zeroes e.g., 01, 02, 03, while D
will use single value e.g., 1, 2, 3.
Solution 3:
You need to specify the input format as the second argument
var inputstartdate = moment(input_value_inputstartdate, "dd-MM-yyyy");
Post a Comment for "Dd-mm-yyyy Date Format With Moment.js"