javascript 如何在javascript中将当前日期时间与另一个日期时间进行比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14645683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I compare the current datetime to another datetime in javascript
提问by Eddie
I am looking for some help, trying to compare 2 dates in JavaScript, but haven't managed to find the exact answer on this site yet.
我正在寻找一些帮助,试图在 JavaScript 中比较 2 个日期,但还没有设法在此站点上找到确切的答案。
I am only just starting out with JavaScript and I get the impression that getting the current date\time is not a simple 1 line of code which I thought it would be. I thought I could use a ternary operator but maybe not. I hope I'm wrong but here is my dilemma.
我刚刚开始使用 JavaScript,我的印象是获取当前日期\时间不是我认为的简单的 1 行代码。我以为我可以使用三元运算符,但也许不能。我希望我错了,但这是我的困境。
I want to compare 2 dates. If date1 is before date2 then do something. If not then do something else.
我想比较 2 个日期。如果 date1 在 date2 之前,那么做一些事情。如果没有,那么做别的事情。
- date1: is the current date time. i.e. my system clock
- date2: is a predefined value. I already have this value in the web page: ${expiredDateTime.format("ddMyyyyhm") I believe date 2 is a string but I'm no expert on jquery.
- date1: 是当前日期时间。即我的系统时钟
- date2: 是一个预定义的值。我在网页中已经有了这个值: ${expiredDateTime.format("ddMyyyyhm") 我相信日期 2 是一个字符串,但我不是 jquery 的专家。
Any help with this would be great.
对此的任何帮助都会很棒。
回答by Sankar V
Please try the below code:
请尝试以下代码:
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");
}
回答by Sankalp Mishra
The easiest way to compare dates in javascript is to first convert it to a Date object and then compare these date-objects.
在 javascript 中比较日期的最简单方法是首先将其转换为 Date 对象,然后比较这些日期对象。
Below you find an object with three functions:
下面是一个具有三个功能的对象:
dates.compare(a,b)
日期.比较(a,b)
Returns a number:
返回一个数字:
-1 if a < b 0 if a = b 1 if a > b NaN if a or b is an illegal date dates.inRange (d,start,end)
-1 if a < b 0 if a = b 1 if a > b NaN 如果 a 或 b 是非法日期 dates.inRange (d,start,end)
Returns a boolean or NaN:
返回布尔值或 NaN:
true if d is between the start and end (inclusive) false if d is before start or after end. NaN if one or more of the dates are illegal. dates.convert
如果 d 在开始和结束之间(包括),则为 true 如果 d 在开始之前或结束之后,则为 false。如果一个或多个日期是非法的,则为 NaN。日期.convert
Used by the other functions to convert their input to a date object. The input can be
由其他函数使用以将其输入转换为日期对象。输入可以是
a date-object : The input is returned as is. 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 : Several different formats is supported, 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.
日期对象:输入按原样返回。数组:解释为 [年、月、日]。注意月份是 0-11。数字:解释为自 1970 年 1 月 1 日以来的毫秒数(时间戳) 字符串:支持几种不同的格式,如“YYYY/MM/DD”、“MM/DD/YYYY”、“2009 年 1 月 31 日”等。 object:解释为具有年、月和日期属性的对象。注意月份是 0-11。
// 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
);
}
}
回答by Orel Eraki
回答by Ripa Saha
try below code for getting current month, year , date
尝试使用以下代码获取当前月份、年份、日期
var date = new Date();
var currentMonth = date.getMonth();
var currentDate = date.getDate();
var currentYear = date.getFullYear();