How to add weeks to date using javascript?

前端 未结 8 698
轻奢々
轻奢々 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:52

    This might not answer the question per se, but one can find a solution with these formulas.

    6.048e+8 = 1 week in milliseconds

    Date.now() = Now in milliseconds

    Date.now() + 6.048e+8 = 1 week from today

    Date.now() + (6.048e+8 * 2) = 2 weeks from today

    new Date( Date.now() + (6.048e+8 * 2) ) = Date Object for 2 weeks from today

    0 讨论(0)
  • 2020-12-29 18:52

    Everything's there !

     date1.toLocaleDateString() 
    

    Thiswill return you date1 as a String in the client convention

    To create a new date date2 with 2 weeks more (2weeks = 2*7*24*60*60 seconds):

     var date2 = new Date(date1 + 60*60*24*7*2);
    
    0 讨论(0)
  • 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
    
    }
    
    0 讨论(0)
  • 2020-12-29 18:57

    You can do this :

    let numWeeks = 2;
    let now = new Date();
    now.setDate(now.getDate() + numWeeks * 7);
    alert(now);
    

    You can see the fiddle here.

    According to the documentation in MDN

    The setDate() method sets the day of the Date object relative to the beginning of the currently set month.

    0 讨论(0)
  • 2020-12-29 19:00

    This should do what you're looking for.

    function LicenceToOccupy(acceptCompletionDate)
    {
        var date1 = new Date(acceptCompletionDate);
        date1.setDate(date1.getDate() + 14);
        document.frmAccept.acceptLicence.value = date1.getDate() + '/' + (date1.getMonth() + 1) + '/' + date1.getFullYear();
    }
    
    0 讨论(0)
  • 2020-12-29 19:03

    You're assigning date1 to be a Date object which represents the string you pass it. What you're seeing in the acceptLicense value is the toString() representation of the date object (try alert(date1.toString()) to see this).

    To output as you want, you'll have to use string concatenation and the various Date methods.

    var formattedDate = date1.getDate() + '/' + (date1.getMonth() + 1) + '/' + date1.getFullYear();
    

    In terms of adding 2 weeks, you should add 14 days to the current date;

    date1.setDate(date.getDate() + 14);
    

    ... this will automatically handle the month increase etc.

    In the end, you'll end up with;

    var date1 = new Date(acceptCompletionDate);
    date1.setDate(date1.getDate() + 14);
    document.frmAccept.acceptLicence.value = date1.getDate() + '/' + (date1.getMonth() + 1) + '/' + date1.getFullYear();
    

    N.B Months in JavaScript are 0-indexed (Jan = 0, Dec = 11), hence the +1 on the month.

    Edit: To address your comment, you should construct date as follows instead, as the Date argument is supposed to be "A string representing an RFC2822 or ISO 8601 date." (see here).

    var segments = acceptCompletionDate.split("/");
    var date1 = new Date(segments[2], segments[1], segments[0]);
    
    0 讨论(0)
提交回复
热议问题