【问题标题】:How to resolve issue NaN-NaN-NaN using datepicker and function?如何使用日期选择器和函数解决问题 NaN-NaN-NaN?
【发布时间】:2020-07-11 21:18:44
【问题描述】:

我正在使用 Datepicker 选择日期和将日期样式转换为所需格式的函数。 选择期间需要的当前格式是使用dd-mm-yyyy20-03-2020。 (我的乡村风格) 而需要使用 Ajax 发送到 API 的格式是 yyyy-mm-dd2020-03-20。所以在提交之前需要进行转换。

在选择期间,我使用带有format: "dd-mm-yyyy", 的日期选择器,如下所示。我还可以使用下面显示的函数将日期从dd-mm-yyyy 转换为yyyy-mm-dd

但是,在提交过程中出现NaN-NaN-NaN 错误(console.log)。如何解决?

HTML

<input type="text" class="form-control" id="actualDate"></input>

日期选择器

$('#actualDate').datepicker({
    language: 'en',
    format: "dd-mm-yyyy",
    clearButton: true,
    toggleSelected: true,
    autoclose: true,
    todayHighlight: true,
    ignoreReadonly: true,
});

JS 函数

$.ajax({
    url : url_projectList + '/1st_api',
    crossDomain: true,
    type : 'POST',
    dataType : 'json',
    data: JSON.stringify({project_id: id}),
    success: function(response){

        var actualDate = $('#actualDate').val();    

        if ($("#actualDate").val() == ""){
            var actual_date = null;  // This is working fine
        } else {
            actual_date = sendDate(actualDate);   // Here is the error coming
        }

        console.log(actual_date)  // I got NaN-NaN-NaN

        $.ajax({
            url : url_projectList + '/2nd_api',
            type : 'POST',
            dataType : 'json',
            data: params,
        });
    }
});

function sendDate(input_date){

    proc_date = new Date(input_date)
    year = proc_date.getYear() + 1900
    month = proc_date.getMonth() + 1
    day = proc_date.getDate()

    if (month < 10)
    {
      month = "0" + month;
    }
    if (day < 10)
    {
      day = "0" + day;
    }

    return year +"-"+ month +"-"+ day;
}

【问题讨论】:

  • 试试var actualDate = $('#actualDate').datepicker('getDate')
  • 不要使用getYear,使用getFullYear
  • 嗨@KresimirPendic,为什么我得到这个日期1970-01-01 当我尝试通过重新提交表单并更改任何日期来编辑表单时。为什么会发生这种情况,它应该会提交原样日期吗?请。

标签: javascript jquery html datetime datepicker


【解决方案1】:
        <input type="text" class="form-control" id="actualDate"/>

        $('input[id$=tbDate]').datepicker({
            language: 'en',
            clearButton: true,
            toggleSelected: true,
            autoclose: true,
            todayHighlight: true,
            ignoreReadonly: true,
            dateFormat: 'yy-mm-dd',
            onSelect: function(dateText) {
                console.log("Selected date: "+ this.value);
            }
        });

        $.ajax({
            url : url_projectList + '/1st_api',
            crossDomain: true,
            type : 'POST',
            dataType : 'json',
            data: JSON.stringify({project_id: id}),
            success: function(response){

                var actualDate = $('#actualDate').datepicker('getDate');  

                if (actualDate == ""){
                    actual_date = null; 
                }

                console.log(actual_date);

                $.ajax({
                    url : url_projectList + '/2nd_api',
                    type : 'POST',
                    dataType : 'json',
                    data: params,
                });
            }
        });

删除了将日期格式化为“yy-mm-dd”格式的额外功能,我使用了 datepicker lib 的 dateFormat 选项

希望对你有帮助:-)

【讨论】:

  • 嗨,[id$=tbDate] 来自哪里?我没有看到任何相关的ID
  • 你能根据我上面的问题尝试回答吗,包括 ID、类名或任何东西。谢谢
  • 您好 mastersuse,抱歉,只需将 [id$=tbDate] 替换为此 [id$=actualDate]。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-17
  • 2014-07-01
  • 2017-03-02
  • 1970-01-01
  • 2015-05-25
  • 2016-03-19
  • 1970-01-01
相关资源
最近更新 更多