Add days to date using javascript

前端 未结 10 1155
执笔经年
执笔经年 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:28
    function onChange(e) {
        var datepicker = $("#DatePicker").val().split("/");
        var joindate = new Date();
        var numberOfDaysToAdd = 1;
        joindate.setFullYear(parseInt(datepicker[2]), parseInt(datepicker[1])-1, parseInt(datepicker[0])+numberOfDaysToAdd);
        $('.new').val(joindate);
    }
    

    http://jsfiddle.net/roberkules/k4GM5/

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

    Is it a typo round joindate.setDate(joindate + numberOfDaysToAdd)?

    I tried this code, it seems ok to me

        var joindate = new Date(2010, 5, 24);
        alert(joindate);
        var numberOfDaysToAdd = 1;
        joindate.setDate(joindate.getDate() + numberOfDaysToAdd);
        var dd = joindate.getDate();
        var mm = joindate.getMonth() + 1;
        var y = joindate.getFullYear();
        var joinFormattedDate = dd + '/' + mm + '/' + y;
        alert(joinFormattedDate);
    
    0 讨论(0)
  • 2020-12-29 12:35

    To answer your real problem, I think your issue is that you're trying to parse the text-value of the DatePicker, when that's not in the right format for your locale.

    Instead of .val(), use:

    var joindate = $('#DatePicker').datepicker("getDate");
    

    to get the underyling Date() object representing the selected date directly from jQuery.

    This guarantees that the date object is correct regardless of the date format specified in the DatePicker or the current locale.

    Then use:

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

    to move it on.

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

    Assuming numberOfDaysToAdd is a number:

    joindate.setDate(joindate.getDate() + numberOfDaysToAdd);
    
    0 讨论(0)
  • 2020-12-29 12:36

    try this.

    Date.prototype.addDay = function(numberOfDaysToAdd){
        this.setTime(this.getTime() + (numberOfDaysToAdd * 86400000));
    };
    
    function onChange(e) {
            var date = new Date(Date.parse($("#DatePicker").val()));
            date.addDay(1);
            var dd = date.getDate();
            var mm = date.getMonth() + 1;
            var y =  date.getFullYear();
            var joinFormattedDate = dd + '/' + mm + '/' + y;
            $('.new').val(joinFormattedDate);
        }
    
    0 讨论(0)
  • 2020-12-29 12:37

    Date('string') will attempt to parse the string as m/d/yyyy. The string 24/06/2011 thus becomes Dec 6, 2012. Reason: 24 is treated as a month... 1 => January 2011, 13 => January 2012 hence 24 => December 2012. I hope you understand what I mean. So:

    var dmy = "24/06/2011".split("/");        // "24/06/2011" should be pulled from $("#DatePicker").val() instead
    var joindate = new Date(
        parseInt(dmy[2], 10),
        parseInt(dmy[1], 10) - 1,
        parseInt(dmy[0], 10)
    );
    alert(joindate);                          // Fri Jun 24 2011 00:00:00 GMT+0500 (West Asia Standard Time) 
    joindate.setDate(joindate.getDate() + 1); // substitute 1 with actual number of days to add
    alert(joindate);                          // Sat Jun 25 2011 00:00:00 GMT+0500 (West Asia Standard Time)
    alert(
        ("0" + joindate.getDate()).slice(-2) + "/" +
        ("0" + (joindate.getMonth() + 1)).slice(-2) + "/" +
        joindate.getFullYear()
    );
    

    Demo here

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