测试日期是否已过去 [英] testing if date is in past

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

问题描述

我如何编写一些Javascript来测试日期是否过去

?我只需要接受当前日期或未来日期。


***通过Developersdex发送 http://www.developersdex.com ***

解决方案

Mike P写道:


如何编写一些Javascript来测试日期是否过去

?我只需要接受当前日期或未来日期。



好​​

var now = new Date();

为您提供当前日期。然后构建第二个日期,例如

var d1 = new Date(2009,0,1);

并比较它们,例如

if(d1 > =现在)


-


Martin Honnen

blockquote>

Martin Honnen写于30 okt 2008在comp.lang.javascript


Mike P写道:


>我如何编写一些Javascript来测试日期是否过去
?我只需要接受当前日期或未来日期。




好​​

var now = new Date();

为您提供当前日期。然后构建第二个日期,例如

var d1 = new Date(2009,0,1);

并比较它们,例如

if(d1 > =现在)



如果现在是2009 / jan / 1 11:33

那么(d1> = now)将是错误。

-

Evertjan。

荷兰。

(请将x''更改为我的电子邮件中的点)


在comp.lang.javascript消息中< 49 ******************** ***@newsspool1.ar

cor-online.net> ;,星期四,2008年10月30日18:44:43,Martin Honnen

< ma **** ***@yahoo.deposted:


> Mike P写道:


var now = new Date();
为您提供当前日期。然后构建第二个日期,例如

var d1 = new Date(2009,0,1);
并比较它们,例如

if(d1> = now )



更好的恕我直言,将d1写为新的日期(2009/01/01)一般因为

的第二个参数新日期(y,m,d)与外部世界中使用的月份 -

编号不匹配。


需要考虑是否存在过去的日子应该被视为

。有人可能想使用now.setHours(0,0,0,0)或

now.setHours(24,0,0,0)---记得在亚速尔群岛有一个

每年不在00:00:00开始,另一天有两次b / b 00:00:00 - 我想。


-

(c)John Stockton,英国伦敦。 ?@merlyn.demon.co.uk Turnpike v6.05。

Web< URL:http://www.merlyn.demon.co.uk/- w。常见问题主题,链接,首字母缩略词

PAS EXE等:< URL:http://www.merlyn.demon.co.uk/programs/-见00index.htm

日期 - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm等


How would I write some Javascript to test whether a date is in the past
or not? I need to only accept either the current date or future dates.

*** Sent via Developersdex http://www.developersdex.com ***

解决方案

Mike P wrote:

How would I write some Javascript to test whether a date is in the past
or not? I need to only accept either the current date or future dates.


Well
var now = new Date();
gives you the current date. Then construct a second date e.g.
var d1 = new Date(2009,0,1);
and compare them e.g.
if (d1 >= now)

--

Martin Honnen
http://JavaScript.FAQTs.com/


Martin Honnen wrote on 30 okt 2008 in comp.lang.javascript:

Mike P wrote:

>How would I write some Javascript to test whether a date is in the past
or not? I need to only accept either the current date or future dates.



Well
var now = new Date();
gives you the current date. Then construct a second date e.g.
var d1 = new Date(2009,0,1);
and compare them e.g.
if (d1 >= now)

if now were 2009/jan/1 11:33
then (d1 >= now) would be false.
--
Evertjan.
The Netherlands.
(Please change the x''es to dots in my emailaddress)


In comp.lang.javascript message <49***********************@newsspool1.ar
cor-online.net>, Thu, 30 Oct 2008 18:44:43, Martin Honnen
<ma*******@yahoo.deposted:

>Mike P wrote:

var now = new Date();
gives you the current date. Then construct a second date e.g.
var d1 = new Date(2009,0,1);
and compare them e.g.
if (d1 >= now)

Better IMHO to write d1 as new Date("2009/01/01") in general because
the second argument of new Date(y,m,d) does not match the month-
numbering used in the Outside World.

One needs to think about whether the present day should be considered as
in the past or not. One may want to use now.setHours(0,0,0,0) or
now.setHours(24,0,0,0) --- remembering that in the Azores there is one
day each year which does not start at 00:00:00 and another that has
00:00:00 twice - I think.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
Web <URL:http://www.merlyn.demon.co.uk/- w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/- see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.


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

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