Jquery / Javascript - Add years to date variable

后端 未结 6 1474
耶瑟儿~
耶瑟儿~ 2021-02-19 06:05

I have a small trouble that would be great to have some help with. I am creating a small form that I want to take a current date formatted \'dd/mm/yyyy\' and add a

相关标签:
6条回答
  • 2021-02-19 06:36
    var dt = new Date($("#startdate").val());
    
    var updateDate = dt.setDate(dt.getFullYear() + $("#registerfor").val());
    
    var dd = updateDate.getDate();
    var mm = updateDate.getMonth();
    var y = updateDate.getFullYear();
    
    var someFormattedDate = dd + '/'+ mm + '/'+ y;
    console.log(someFormattedDate);
    
    0 讨论(0)
  • 2021-02-19 06:36

    I would:

    • put the date in a format that Date.parse can work nicely with (mm/dd/yyyy)
    • add the number of milliseconds in a year times the selected option
    • Reformat the new value to a human-readable format and populate #expires

      var dateArr = $("#startdate").val().split();
      var niceDate = Number(dateArr[1]) + "/" + Number(dateArr[0]) + "/" + Number(dateArr[2]);

      var expDate = Date.parse(niceDate) + 365*24*60*60*1000*Number($("#registerfor").val());

      $("#expires").val(expDate.getMonth()+1 + "/" + expDate.getDate() + "/" + expDate.getFullYear());

    If you need 2-digit days and months for expiration date, you could have extra variables that hold a string version of them and check something like:
    var padMonth = expDate.getMonth()+1;
    if(expDate.getMonth()+1 < 10) padMonth = "0" + Number(expDate.getMonth()+1);

    After which, you would use padMonth in $("#expires").val(...)

    0 讨论(0)
  • 2021-02-19 06:41

    Dude, it's already working. If you still want to convert in date then do this:

    var dat=Date.parse(startdate );
    
    0 讨论(0)
  • 2021-02-19 06:48

    Sometimes what looks like quite a straight forward task becomes quite complicated.

     $(document).ready( function () {
    
        $("#registerfor, #startdate").change( function () {
    
            var str = $("#startdate").val();
    
            if( /^\d{2}\/\d{2}\/\d{4}$/i.test( str ) ) {
    
                var parts = str.split("/");
    
                var day = parts[0] && parseInt( parts[0], 10 );
                var month = parts[1] && parseInt( parts[1], 10 );
                var year = parts[2] && parseInt( parts[2], 10 );
                var duration = parseInt( $("#registerfor").val(), 10);
    
                if( day <= 31 && day >= 1 && month <= 12 && month >= 1 ) {
    
                    var expiryDate = new Date( year, month - 1, day );
                    expiryDate.setFullYear( expiryDate.getFullYear() + duration );
    
                    var day = ( '0' + expiryDate.getDate() ).slice( -2 );
                    var month = ( '0' + ( expiryDate.getMonth() + 1 ) ).slice( -2 );
                    var year = expiryDate.getFullYear();
    
                    $("#expires").val( day + "/" + month + "/" + year );
    
                } else {
                    // display error message
                }
            }
        });
    });
    

    Here is a fiddle so you can see it in action.

    0 讨论(0)
  • 2021-02-19 06:54

    A quick and easy solution :

    var myDate = new Date();
    myDate.setFullYear(myDate.getFullYear() + nbYearsToAdd);
    

    "nbYearsToAdd" can be negative.

    0 讨论(0)
  • 2021-02-19 06:57

    lots of date picker jquery are available on the net

    This

    one is my favourite, easy to implement and edit as per our needs.

    if you want to use script from your code, you can directly pass the date value to var startdate like

    var startdate=<?php echo date("Y-m-d")?>
    

    try this.. may help.

    0 讨论(0)
提交回复
热议问题