2016-05-26 7 views
0

私はこのコードを持っている:Javascriptの有効期限の検証ステータス

$(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でこれをどのように解決できますか?次のコードは、タイムスタンプが最後の日付は有効期限が切れたか、まだ有効であるかどうかを見つけるために、今日の日付と計算され、最後の日との間で比較されているでは

はあなたに

+0

こんにちは、あなたはこれを参考にしてください。誰かがこれを試して解決しやすくなります。 – thepio

+0

ライブラリやフレームワークにはタグは付けられていませんが、使用されているものもあります。あなたがそうするなら、それは役に立ちます。 – RobG

+0

* lastDay *は3回宣言され、* lastMonth *と* lastDay *は一切宣言されていないため、コード実行時にグローバルになります。 'isNaN(lastMonth)'はいつ真実でしょうか?どのような価値が '$("#x_installeddate ")ですか?val()' return? – RobG

答えて

0

ありがとうございます。

こちらがお役に立てば幸いです。

$(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 
     } 
    }); 
}); 
+0

statusは、データベース内の別の列です(例:column:有効期限の値:2016-05-25カラム:ステータス値:1 – Laj

+0

@Laj) 'else'ループの中でajax呼び出しを使用して、バックエンドスクリプト(例:filename.php)を使用して、必要に応じてDB列を更新します。 – dinesh

0

コードは、必要以上に複雑で非効率的です。私は$("#x_installeddate").val()からの戻り値が正しく新しい日付によって解析されていると仮定しますが、これは問題があります。ここでは、日付の解析について多くの質問があります。

あなたが今日と比較し、選択した日付に基づいて日付を生成したい場合はとにかく、次の点を考慮してください

$(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年ごとに返します。

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

をしてオブジェクトを削除します。次に、あなたが行うことができます。

ではなくlastDateの変形して使用することができるよう日付変数は、再利用されません。

関連する問題