【问题标题】:Javascript Expiration Date Validate StatusJavascript 到期日期验证状态
【发布时间】:2016-05-26 05:18:36
【问题描述】:

我有这个代码:

    $(document).ready(function() {
        $('input[name=x_contract]:radio').click(function(){
            var date = new Date($("#x_installeddate").val());
            if($(this).attr("value")=="Yearly"){
                var lastDay = new Date(date.getFullYear(), date.getMonth() + 12, date.getDate());            
            } else if ($(this).attr("value") == "2 Years") {
                var lastDay = new Date(date.getFullYear(), date.getMonth() + 24, date.getDate()); 
            } else if ($(this).attr("value") == "3 Years") {
                var lastDay = new Date(date.getFullYear(), date.getMonth() + 36, date.getDate()); 
        }
        lastMonth = ( (lastDay.getMonth() + 1) < 10) ? '0' + (lastDay.getMonth() + 1) : (lastDay.getMonth() + 1);
        lastDate = ( lastDay.getDate() < 10 ) ? '0' + lastDay.getDate() : lastDay.getDate();
        var newDate =  lastDay.getFullYear() + '/' + lastMonth + '/' + lastDate;
        if (isNaN(lastMonth) == false) $("#x_expirationdate").val(newDate);
    });
});

此函数每年、2 年和 3 年生成一个到期日期。在此函数生成到期日期后,我想要更新状态,无论它是否已过期。这取决于当前日期。如果日期已过期,我希望其他列状态为 1,否则为 0。

如何在 JavaScript 中解决这个问题 :) ?

谢谢

【问题讨论】:

  • 您好,请您制作一个小提琴/示例。会让别人更容易尝试解决这个问题。
  • 您尚未标记任何库或框架,但显然有些正在使用。这样做会很有帮助。
  • lastDay 被声明了 3 次,lastMonthlastDay 根本没有被声明,所以当代码变成全局的运行。 isNaN(lastMonth) 什么时候是真的? $("#x_installeddate").val() 返回什么值?

标签: javascript validation date status


【解决方案1】:

在以下代码中,将比较今天日期和计算的最后日期之间的时间戳,以确定最后日期是否已过期或仍然有效。

我希望这会有所帮助。

$(document).ready(function() {
    $('input[name=x_contract]:radio').click(function(){
        var date = new Date($("#x_installeddate").val());
        var today = new Date(), todayTimeStamp = 0, lastDayTimeStamp = 0;

        if($(this).attr("value")=="Yearly"){
            var lastDay = new Date(date.getFullYear(), date.getMonth() + 12, date.getDate());            
        } else if ($(this).attr("value") == "2 Years") {
            var lastDay = new Date(date.getFullYear(), date.getMonth() + 24, date.getDate()); 
        } else if ($(this).attr("value") == "3 Years") {
            var lastDay = new Date(date.getFullYear(), date.getMonth() + 36, date.getDate()); 
        }
        lastMonth = ( (lastDay.getMonth() + 1) < 10) ? '0' + (lastDay.getMonth() + 1) : (lastDay.getMonth() + 1);
        lastDate = ( lastDay.getDate() < 10 ) ? '0' + lastDay.getDate() : lastDay.getDate();
        var newDate =  lastDay.getFullYear() + '/' + lastMonth + '/' + lastDate;
        if (isNaN(lastMonth) == false) $("#x_expirationdate").val(newDate);

        todayTimeStamp = today.getTime();
        lastDayTimeStamp = lastDay.getTime();

        if(todayTimeStamp > lastDayTimeStamp) {
            // expired
            // status is 1
        } else {
            // Valid
            // status is 0
        }
    });
});

【讨论】:

  • status 是数据库中的另一列,例如列:到期日期值:2016-05-25 列:状态值:1
  • @Laj 您可以在if else 循环内使用ajax 调用将更新发送到后端脚本(例如:filename.php)以根据需要更新DB 列.
【解决方案2】:

您的代码比它需要的更加复杂和低效。我假设$("#x_installeddate").val() 的返回值被new Date 正确解析,这是有问题的。这里有很多关于解析日期的问题。

无论如何,如果您想根据所选日期生成日期,然后将其与今天进行比较,请考虑以下几点:

$(document).ready(function() {
  $('input[name=x_contract]:radio').click(function checkDate() {

    // Create a date for the selected date
    var date = new Date($("#x_installeddate").val());

    // Create a date for the selected period ahead from
    // the selected date    
    var values = {'Yearly':12, '2 Years':24, '3 Years':36};
    var lastDate = new Date(+date);
    lastDate.setMonth(lastDate.getMonth() + values[this.value]);

    // Create a string for lastDate
    var newDate = lastDate.getFullYear() + '/' +
                  ('0'+(lastDate.getMonth() + 1)).slice(-2) + '/' +
                  ('0'+lastDate.getDate()).slice(-2);

    // Get the current date and compare with lastDate
    var now = new Date();
    if (now < lastDate) {
      // lastDate is in the future
    } else {
      // lastDate is today or earlier
    }
  });
});

如果单选按钮的值是年份,那么它们分别在 1、2 和 3 年返回值 1、2 和 3 会更好。然后你可以这样做:

lastDate.setMonth(lastDate.getMonth() + this.value*12);

并删除 values 对象。

date 变量没有被重用,因此可以修改和使用它来代替 lastDate

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2021-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多