moment.js isBetween() 方法使用澳大利亚日期格式
moment.js isBetween() method to use Aus date format
我知道如何使用 moment.js
格式化我的日期,但是当我阅读有关 isBetween()
方法的文档时,它声明要传递 2 个格式为 YYYY/MM/DD
的日期我的日期都设置为 DD/MM/YYYY
如何让 isBetween() 方法识别澳大利亚日期格式。
http://momentjs.com/docs/#/query/is-between/
我的代码:
<script src="//code.jquery.com/jquery-1.11.3.js"></script>
<script src="js/moment-with-locales.js"></script>
<script src="js/moment-timezone-with-data.min.js"></script>
//checking to see if the user is 16 or 17 years old.
function parentGuardianRequired() {
//todays date generated in a hidden field via php
var signDate = $("#currentDate").val();
//the users date of birth input
var dateOfBirth = $("#dateOfBirth").val();
//using moment to get the exact time of these dates while formatting the dates to AUS.
var birthDate = moment(dateOfBirth, "DD/MM/YYYY");
var currentDate = moment(signDate, "DD/MM/YYYY");
//subtracting 16 and 18 years from todays date to always do a valid age check
ageSixteen = currentDate.subtract(16, "years");
ageSeventeen = currentDate.subtract(18, "years");
//checks if the user is between the age of 16 and 17
if (birthDate.isBetween(ageSixteen, ageSeventeen)) {
//you are 16 or 17 years old
console.log("TRUE");
} else {
//You are not 16 or 17 years old
console.log("FALSE");
}
}
moment.isBetween()
适用于两个 moment
对象。这里的问题是您在同一个对象上使用 subtract
两次,只是 mutates it each time。它不是 return 新对象。所以你的代码应该看起来更像这样:
ageSixteen = moment(currentDate).subtract(16, "years");
ageSeventeen = moment(currentDate).subtract(18, "years");
我知道如何使用 moment.js
格式化我的日期,但是当我阅读有关 isBetween()
方法的文档时,它声明要传递 2 个格式为 YYYY/MM/DD
的日期我的日期都设置为 DD/MM/YYYY
如何让 isBetween() 方法识别澳大利亚日期格式。
http://momentjs.com/docs/#/query/is-between/
我的代码:
<script src="//code.jquery.com/jquery-1.11.3.js"></script>
<script src="js/moment-with-locales.js"></script>
<script src="js/moment-timezone-with-data.min.js"></script>
//checking to see if the user is 16 or 17 years old.
function parentGuardianRequired() {
//todays date generated in a hidden field via php
var signDate = $("#currentDate").val();
//the users date of birth input
var dateOfBirth = $("#dateOfBirth").val();
//using moment to get the exact time of these dates while formatting the dates to AUS.
var birthDate = moment(dateOfBirth, "DD/MM/YYYY");
var currentDate = moment(signDate, "DD/MM/YYYY");
//subtracting 16 and 18 years from todays date to always do a valid age check
ageSixteen = currentDate.subtract(16, "years");
ageSeventeen = currentDate.subtract(18, "years");
//checks if the user is between the age of 16 and 17
if (birthDate.isBetween(ageSixteen, ageSeventeen)) {
//you are 16 or 17 years old
console.log("TRUE");
} else {
//You are not 16 or 17 years old
console.log("FALSE");
}
}
moment.isBetween()
适用于两个 moment
对象。这里的问题是您在同一个对象上使用 subtract
两次,只是 mutates it each time。它不是 return 新对象。所以你的代码应该看起来更像这样:
ageSixteen = moment(currentDate).subtract(16, "years");
ageSeventeen = moment(currentDate).subtract(18, "years");