Convert dd-mm-yyyy string to date

后端 未结 14 1940
孤城傲影
孤城傲影 2020-11-22 05:09

i am trying to convert a string in the format dd-mm-yyyy into a date object in JavaScript using the following:

 var from = $(\"#datepicker\").val();
 var to          


        
14条回答
  •  情话喂你
    2020-11-22 05:58

    You could use a Regexp.

    var result = /^(\d{2})-(\d{2})-(\d{4})$/.exec($("#datepicker").val());
    if (result) {
        from = new Date(
            parseInt(result[3], 10), 
            parseInt(result[2], 10) - 1, 
            parseInt(result[1], 10)
        );
    }
    

提交回复
热议问题