![](/img/trans.png)
[英]Need to use javascript to tell me elasped days,month, and years from current date to date entered
[英]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.