简体   繁体   中英

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

moment.min.js:6 Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.

$(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);

        var portalexpdate = inputstartdate.add(input_value_inputmonthadded, 'months');

        var formatedportalexpdate = portalexpdate.format('dd-MM-yyyy');

        $('#inputportalexpirydate').val(formatedportalexpdate);

    }
    else {

        $('#inputportalexpirydate').val('');
    }

});

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(''); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script> <input type="number" id="inputmonthadded"> <input type="text" id="inputstartdate" value="12-September-2017" readonly> <input type="text" id="inputportalexpirydate" readonly> <input type="text" id="inputmonthpurchased"> 

You need to follow moment's formatting style: https://momentjs.com/docs/#/displaying/

Therefore: format("D-MMMM-YYYY")

Note: using DD will append zeroes eg, 01, 02, 03, while D will use single value eg, 1, 2, 3.

You need to specify the input format as the second argument

var inputstartdate = moment(input_value_inputstartdate, "dd-MM-yyyy");

https://momentjs.com/docs/#/parsing/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM