繁体   English   中英

检查日期是否大于指定的日期

[英]Check if a date is greater than specified date

如何仅使用MMM-YYYY格式传递月份和年份,如何使用javascript检查日期是否大于或小于指定的日期。

例如 :

让用户从下拉月份和2016年,从下拉列表中选择JUL,

现在,如何检查所选值是否大于/小于 2016年6月。

我已经将Session变量中的输入作为session(“ month”)和session(“ yrs”)。

我尝试了以下方法,但没有任何反应:

var d1 = new Date('JUN-2016')
var d2 = new Date(<% =session("month")+"-"+session("yrs") %>) )
if(d2.getTime() > d2.getTime())
{
alert('Greater than jun 2016');
}
else
{
alert('Less than jun 2016');
}

我已经通过此链接比较了两个日期与JavaScript,但没有找到适合我的情况的解决方案。

由于此应用程序是在VB上开发的,因此我对此并不了解。

我该如何解决。

请建议

提前致谢。

尝试这个 -

var x = new Date('JUN-2016');
var y = new Date('MAY-2016');
console.log(+x < +y);
console.log(+x > +y);
console.log(+x === +y);

您只需要对日期进行简单比较。 请在此处阅读有关“日期”的更多信息: http : //www.w3schools.com/js/js_date_methods.asp

也许这段代码可能会有所帮助:

var day = 1; // Since you don't care about the day just use the first day
var month = session("month"); // Use the number of the month instead of the name
var year = session("yrs");
var dateJune = Date.parse("2016-07-01"); // June
var dateInput = Date.parse(year + "-" + month + "-" + day);
// Compare dates
if (dateJune > dateInput) {
    // The input is before june
} else if (dateJune < dateInput) {
    // The input is after june
} else {
    // It is june
}

您需要一种有效的日期格式才能解析日期。 而不是从你的选择框,让“君”,这将是更好地得到月份的数量来代替。 一个选择框应该是这样的:

<select>
<option value="01">Jan</option>
<option value="02">Feb</option>
...
</select>

如果这不是一个选项,您可以使用计算数量为你,如果你知道那么字符串值,你一个月变量可能有一个函数:

function (nameOfMonth) {
   switch (nameOfMonth) {
     case 'Jan':
        return "01";
     case 'Feb':
        return "02";
     ....
   }
}

首先将其转换为日期对象

  // convert to a parseable date string:
   var dateStrA = "28/12/2013 16:20:22".replace( /(\d{2})\/(\d{2})\/(\d{4})/,        "$2/$1/$3");
    var dateStrB = "28/12/2013 16:20:11".replace( /(\d{2})\/(\d{2})\/(\d{4})/,        "$2/$1/$3");

     // now you can compare them using:
      new Date(dateStrA) > new Date(dateStrB);
var date1 = Math.ceil(Math.abs(first date) / (1000 * 3600 * 24));
var date2 = Math.ceil(Math.abs(second date) / (1000 * 3600 * 24));
if(date1  > date2 )
   alert("date1");
else
   alert("date2");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM