簡體   English   中英

在javascript中檢查輸入的日期是否為18年,帶天

[英]check whether the entered date is 18 years with days in javascript

我需要檢查輸入的日期是否大於18歲,或者不使用以下代碼

today = new Date();
          birthday_val = moment(self.$el.find(".Custdb").val(), "D-MMM-YYYY").toDate();
          birthday_val_months = birthday_val.getMonth();
          birthday_val_days = birthday_val.getDay();
          age = today.getFullYear() - birthday_val.getFullYear();
          if (today.getMonth() < birthday_val_months || (today.getMonth() === birthday_val_months && today.getDay() < birthday_val_days)) {
            age--;
          }

if(age<=18)   
{Age should be greater that 18 years}

如果我選擇1998年1月1日,則其顯示的味精年齡應大於18歲

這個經過稍微修改的代碼版本可以達到目的:

today = new Date();
birthday_val = moment(self.$el.find(".Custdb").val(), "D-MMM-YYYY").toDate();
age = today.getFullYear() - birthday_val.getFullYear();
month = today.getMonth() - birthDate.getMonth();
if (month < 0 || (month === 0 && today.getDate() < birthday_val.getDate()))   {
    age--;
}    

if(age<=18)   
{
   alert('You should be over 18 to use this site')
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM