JavaScript实现一个函数判断年龄是否18?

比如传入'1991-6-3'返回true
传递'2001-6-3'返回false

慕仙森
浏览 1189回答 1
1回答

拉丁的传说

function getAge(dateString) {&nbsp; &nbsp; var today = new Date();&nbsp; &nbsp; var birthDate = new Date(dateString);&nbsp; &nbsp; var age = today.getFullYear() - birthDate.getFullYear();&nbsp; &nbsp; var m = today.getMonth() - birthDate.getMonth();&nbsp; &nbsp; if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {&nbsp; &nbsp; &nbsp; &nbsp; age--;&nbsp; &nbsp; }&nbsp; &nbsp; return age > 18;}console.log(getAge('1997-11-09'))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript