如何使用JavaScript检测日期是否为过去? [英] How to detect if the date is in the past using JavaScript?

查看:79
本文介绍了如何使用JavaScript检测日期是否为过去?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用javascript检查日期是否过去.

I need to check if the date is in the past using javascript.

日期格式如下:10/06/2018

我可以使用以下代码执行此操作:

I can do this using the following code:

var datep = '10/06/2018';
if(Date.parse(datep)-Date.parse(new Date()) < 0) {
   alert('date is in the past');
}

但是此代码似乎仅检查Year.因此,如果MonthDay是过去的版本,它将忽略该内容!

But this code seems to only check for Year. So if the Month or the Day is in the past, it will ignore that!

有人可以在how to check for the day and month as well as the year上提供建议吗?

Could someone please advice on how to check for the day and month as well as the year?

先谢谢了.

日期格式为

dd/mm/yyyy 

推荐答案

将其设为两个数字并进行比较

Make it two numbers and compare

var date1 ='05/06/2018';
date1=parseInt(date1.split('/').reverse().join(''));
//number 20180605

var date2 = new Date();
date2 = parseInt(date2.toISOString().slice(0,10).replace(/-/g,""));
// number 20180610

//compare
if(date1<date2)alert('in the past');

这篇关于如何使用JavaScript检测日期是否为过去?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆