How to add weeks to date using javascript?

前端 未结 8 801
轻奢々
轻奢々 2020-12-29 18:32

Javascript definitely isn\'t my strongest point. I\'ve been attempting this for a couple of hours now and seem to be getting stuck with date formatting somewhere.

I

8条回答
  •  [愿得一人]
    2020-12-29 18:53

    To parse the specific dd/mm/yyyy format and increment days with 14 , you can do something like split the parts, and create the date object with y/m/d given specfically. (incrementing the days right away) Providing the separator is always -, the following should work:

    function LicenceToOccupy(acceptCompletionDate)
    {
        var parts = acceptCompletionDate.split("/");
        var date1 = new Date(parts[2], (parts[1] - 1), parseInt(parts[0]) + 14); //month 0 based, day: parse to int and increment 14 (2 weeks)
        document.frmAccept.acceptLicence.value = date1.toLocaleDateString(); //if the d/m/y format is the local string, otherwise some cusom formatting needs to be done
    
    }
    

提交回复
热议问题