如何在 javascript 中将当前日期时间与另一个日期时间进行比较

How do I compare the current datetime to another datetime in javascript

本文关键字:时间 另一个 日期 比较 当前日期 javascript      更新时间:2023-09-26

我正在寻求帮助,试图在 JavaScript 中比较 2 个日期,但尚未设法在此站点上找到确切答案。

我才刚刚开始使用 JavaScript,我的印象是获取当前日期''时间不是我认为的简单的 1 行代码。我以为我可以使用三元运算符,但也许不能。我希望我错了,但这是我的困境。

我想比较 2 个日期。如果 date1 早于 date2,则执行某些操作。如果没有,那就做别的事情。

  • date1:是当前日期时间,即我的系统时钟
  • date2:是预定义的值。我已经在网页中有这个值:${expireDateTime.format("ddMyyyyhm") 我相信日期 2 是一个字符串,但我不是 jquery 的专家。

对此有任何帮助都会很棒。

请尝试以下代码:

var user="12/11/2012/5/30";
var arrdt= user.split("/");
var userdt = new Date(arrdt[2], arrdt[1] - 1, arrdt[0],arrdt[3],arrdt[4]);
var currdt = new Date();
if (userdt < currdt) {
alert("userdate is before current date");
}else{
alert("userdate is after current date");
}

在javascript中比较日期的最简单方法是首先将其转换为Date对象,然后比较这些日期对象。

下面是一个具有三个函数的对象:

日期比较(a,b)

返回一个数字:

-1 如果 a

 // Source: http://stackoverflow.com/questions/497790
    var dates = {
        convert:function(d) {
            // Converts the date in d to a date-object. The input can be:
            //   a date object: returned without modification
            //  an array      : Interpreted as [year,month,day]. NOTE: month is 0-11.
            //   a number     : Interpreted as number of milliseconds
            //                  since 1 Jan 1970 (a timestamp) 
            //   a string     : Any format supported by the javascript engine, like
            //                  "YYYY/MM/DD", "MM/DD/YYYY", "Jan 31 2009" etc.
            //  an object     : Interpreted as an object with year, month and date
            //                  attributes.  **NOTE** month is 0-11.
            return (
                d.constructor === Date ? d :
                d.constructor === Array ? new Date(d[0],d[1],d[2]) :
                d.constructor === Number ? new Date(d) :
                d.constructor === String ? new Date(d) :
                typeof d === "object" ? new Date(d.year,d.month,d.date) :
                NaN
            );
        },
        compare:function(a,b) {
            // Compare two dates (could be of any type supported by the convert
            // function above) and returns:
            //  -1 : if a < b
            //   0 : if a = b
            //   1 : if a > b
            // NaN : if a or b is an illegal date
            // NOTE: The code inside isFinite does an assignment (=).
            return (
                isFinite(a=this.convert(a).valueOf()) &&
                isFinite(b=this.convert(b).valueOf()) ?
                (a>b)-(a<b) :
                NaN
            );
        },
        inRange:function(d,start,end) {
            // Checks if date in d is between dates in start and end.
            // Returns a boolean or NaN:
            //    true  : if d is between start and end (inclusive)
            //    false : if d is before start or after end
            //    NaN   : if one or more of the dates is illegal.
            // NOTE: The code inside isFinite does an assignment (=).
           return (
                isFinite(d=this.convert(d).valueOf()) &&
                isFinite(start=this.convert(start).valueOf()) &&
                isFinite(end=this.convert(end).valueOf()) ?
                start <= d && d <= end :
                NaN
            );
        }
    }

链接的可能重复

if (date1.getTime() > date2.getTime()) {
    alert("The first date is after the second date!");
}

对日期对象的引用

尝试以下代码以获取当前月份,年份,日期

var date = new Date();
var currentMonth = date.getMonth();
var currentDate = date.getDate();
var currentYear = date.getFullYear();