在jQuery中将日期添加到给定日期[重复]

时间:2022-08-25 17:43:50

This question already has an answer here:

这个问题在这里已有答案:

I have a form with three fields, "start_date", "days", "end_date". I would like to get the end date by adding days to the start date.

我有一个包含三个字段的表单,“start_date”,“days”,“end_date”。我想通过在开始日期添加天数来获得结束日期。

My jQuery code is:

我的jQuery代码是:

$("#days").change(function(){
  var start_date = new Date($("#start_date").attr('value'));
  var days = parseInt($("#days").attr('value'))-1;
  var end_date = new Date(start_date);
  end_date.setDate(start_date.getDate() + days);                            
  $("#end_date").val(end_date.getFullYear() + '-' + ("0" + (end_date.getMonth() + 1)).slice(-2) + '-' + ("0" + end_date.getDate()).slice(-2));
});

In the "end_date" field I get "NaN-aN-aN".

在“end_date”字段中,我得到“NaN-aN-aN”。

What am I doing wrong?

我究竟做错了什么?

1 个解决方案

#1


17  

NaN stands for Not a Number - This means that the users input is invalid.

NaN代表非数字 - 这意味着用户输入无效。

You could check to see if the input is valid by using if(!isNaN(date.getTime())){

您可以使用if(!isNaN(date.getTime())){来查看输入是否有效。

Some code to get you started:

一些代码可以帮助您入门:

;(function($, window, document, undefined){
    $("#days").on("change", function(){
       var date = new Date($("#start_date").val()),
           days = parseInt($("#days").val(), 10);

        if(!isNaN(date.getTime())){
            date.setDate(date.getDate() + days);

            $("#end_date").val(date.toInputFormat());
        } else {
            alert("Invalid Date");  
        }
    });


    //From: http://*.com/questions/3066586/get-string-in-yyyymmdd-format-from-js-date-object
    Date.prototype.toInputFormat = function() {
       var yyyy = this.getFullYear().toString();
       var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based
       var dd  = this.getDate().toString();
       return yyyy + "-" + (mm[1]?mm:"0"+mm[0]) + "-" + (dd[1]?dd:"0"+dd[0]); // padding
    };
})(jQuery, this, document);

http://jsfiddle.net/MCzJ6/1

Hope this helps.

希望这可以帮助。

W

#1


17  

NaN stands for Not a Number - This means that the users input is invalid.

NaN代表非数字 - 这意味着用户输入无效。

You could check to see if the input is valid by using if(!isNaN(date.getTime())){

您可以使用if(!isNaN(date.getTime())){来查看输入是否有效。

Some code to get you started:

一些代码可以帮助您入门:

;(function($, window, document, undefined){
    $("#days").on("change", function(){
       var date = new Date($("#start_date").val()),
           days = parseInt($("#days").val(), 10);

        if(!isNaN(date.getTime())){
            date.setDate(date.getDate() + days);

            $("#end_date").val(date.toInputFormat());
        } else {
            alert("Invalid Date");  
        }
    });


    //From: http://*.com/questions/3066586/get-string-in-yyyymmdd-format-from-js-date-object
    Date.prototype.toInputFormat = function() {
       var yyyy = this.getFullYear().toString();
       var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based
       var dd  = this.getDate().toString();
       return yyyy + "-" + (mm[1]?mm:"0"+mm[0]) + "-" + (dd[1]?dd:"0"+dd[0]); // padding
    };
})(jQuery, this, document);

http://jsfiddle.net/MCzJ6/1

Hope this helps.

希望这可以帮助。

W