Add days to date using javascript

前端 未结 10 1156
执笔经年
执笔经年 2020-12-29 12:21

I am trying to add days to a given date using javascript. I have the following code

function onChange(e) {
    var datepicker = $(\"#DatePicker\").val();
           


        
相关标签:
10条回答
  • 2020-12-29 12:41

    I would like to encourage you to use DateJS library. It is really awesome!

    function onChange(e) {
        var date = Date.parse($("#DatePicker").val()); //You might want to tweak this to as per your needs.
        var new_date = date.add(n).days();
        $('.new').val(new_date.toString('M/d/yyyy'); //You might want to tweak this as per your needs as well.
    }
    
    0 讨论(0)
  • 2020-12-29 12:43
    Date.prototype.addDays = function(days) {
        this.setDate(this.getDate() + days);
        return this;
    };
    

    and in your javascript code you could call

    var currentDate = new Date();
    // to add 8 days to current date
    currentDate.addDays(8);
    
    0 讨论(0)
  • 2020-12-29 12:48

    Try

    function onChange(e) {
            var datepicker = $("#DatePicker").val();
            alert(datepicker);
            var parts = datepicker.split(/[^\d]/);
            var joindate = new Date();
            joindate.setFullYear(parts[2], parts[1]-1, parts[0]);
            alert(joindate);
            var numberOfDaysToAdd = 1;
            joindate.setDate(joindate + numberOfDaysToAdd);
            var dd = joindate.getDate();
            var mm = joindate.getMonth() + 1;
            var y = joindate.getFullYear();
            var joinFormattedDate = dd + '/' + mm + '/' + y;
            $('.new').val(joinFormattedDate);
    
        }
    

    I suppose the problem is JavaScript expects format MM/DD/YYYY not DD/MM/YYYY when passed into Date constructor.

    0 讨论(0)
  • 2020-12-29 12:55

    The first alert is the value of the field. the second is the generated date from a non-US formatted date.

    Here is a working example

    (seems that this kind of markup is necessary to get noticed)

    If you want to keep your code, then you need to change

    var joindate = new Date(datepicker);

    to

    var parms = datepicker.split("/");
    

    then use

    var joindate = new Date(parms[1]+"/"+parms[0]+"/"+parms[2]);
    

    OR the identically working

    var joindate = new Date(parms[2],parms[1]-1,parms[0]);
    

    As pointed out in a few other answers too, use the .getDate()

    joindate.setDate(joindate.getDate() + numberOfDaysToAdd);
    

    Lastly you want to add a 0 if the month is < 10

    if (mm<10) mm="0"+mm;
    

    If you are using the datepicker from jQuery UI, then you can do

    $('.new').val($("#DatePicker").datepicker( "setDate" , +1 ).val());

    instead of your function

    http://jqueryui.com/demos/datepicker/#method-setDate

    Sets the current date for the datepicker. The new date may be a Date object or a string in the current date format (e.g. '01/26/2009'), a number of days from today (e.g. +7) or a string of values and periods ('y' for years, 'm' for months, 'w' for weeks, 'd' for days, e.g. '+1m +7d'), or null to clear the selected date.

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