问题描述
我正在寻找一个移动网站的验证,我有两个输入字段,我希望第一个验证值不迟于今天的日期,第二个验证它不迟于一个提前一年的第一个值。
例如
- 第一个值= 26 / 11/2013
- 第二个值不能包含晚于26/11/2014的值
我喜欢。它可以更容易地处理日期和时间。
首先,让我们确保一天在明天之前。这将取决于明天的定义是什么。
var m = moment(26/11/2013, MM / DD / YYYY);
//明天这次
var t = moment()。add(days,1);
//明天开始
var明天=时刻([t.year(),t.month(),t.date()]);
if(m.lessThan(明天)){
//今天!!! (或之前)
}
同样,相同的方法可以使用一年现在。在这种情况下,它可能不够关心时间组件,而且我在另一天徘徊 - 但如果它很重要(例如,寻找当天的开始),请参阅前面的示例。
var m = moment(26/11/2013,MM / DD / YYYY);
var aYearFromNow = moment()。add(years,1).add(days,1);
if(m.lessThan(aYearFromNow)){
//还不到一年!
}
I'm looking to implement validation for a mobile site, where I have two input fields and I would like the first to validate the value is no later than todays date, and the second to validate it is no later than a one year in advance of the first value.
E.g
- First Value = 26/11/2013
- Second Value can not contain a value later than 26/11/2014
Is this possible?
I like moment.js. It makes it easier to deal with dates and times.
First, let's make sure a day "is before tomorrow". This will depend a bit upon what the definition of tomorrow is.
var m = moment("26/11/2013", "MM/DD/YYYY");
// tomorrow this time
var t = moment().add("days", 1);
// tomorrow start of day
var tomorrow = moment([t.year(), t.month(), t.date()]);
if (m.lessThan(tomorrow)) {
// today!!! (or before)
}
Similarly, the same approach can be used for a year from now. It's likely fine enough to not care about the time component in this case, and I've slogged on another day - but if it matters (e.g. looking for the start of the day), see the previous example.
var m = moment("26/11/2013", "MM/DD/YYYY");
var aYearFromNow = moment().add("years", 1).add("days", 1);
if (m.lessThan(aYearFromNow)) {
// still less than a year!
}
这篇关于HTML5日期验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!