日期数学 [英] Date math

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

问题描述

有些日期计算有问题。我需要在一个月内获得特定日期的日期 - 第一个星期四或第二个星期一等。我

找到一篇文章( http://www.dotnetspider.com/kb/Article1252.aspx

给出了在DateAdd()和DateDiff()函数中使用的示例,但似乎

并非所有示例都按照描述工作....当然,最后一个

文章中的示例非常接近我需要的但是不起作用(我运行时我会继续获得08/11/0287)。


我的目标是有一种方法可以判断它是否是一个月的特定日期所以

我可以向用户显示一条消息。我只是无法得到我的头

在必要的日期数学周围。


非常感谢任何帮助。谢谢


- Andrew

Having trouble with some date computations. I need to get the date of a
specific day within a month - the 1st Thursday or the 2nd Monday, etc. I
found an article ( http://www.dotnetspider.com/kb/Article1252.aspx) that
gives examples using on the DateAdd() and DateDiff() functions, but it seems
that not all of the examples work as described.... and of course, the last
example in the article is very close to what I need yet does not work (I
keep getting 08/11/0287 when I run it).

My goal is to have a way I can tell if it is a specific day of the month so
I can display a message to the user. I am just not able to get my head
around the necessary date math.

Any help is greatly appreciated. Thanks

-- Andrew

推荐答案

" Andrew" < An **** @ somewhereoutthere.com写在留言中

news:uH ************** @ TK2MSFTNGP03.phx.gbl ...
"Andrew" <An****@somewhereoutthere.comwrote in message
news:uH**************@TK2MSFTNGP03.phx.gbl...

有些日期计算有问题。我需要在一个月内获得特定日期的日期 - 第一个星期四或第二个星期一等。我

找到一篇文章( http://www.dotnetspider.com/kb/Article1252.aspx

给出了在DateAdd()和DateDiff()函数中使用的示例,但它似乎并非所有示例都按照描述工作....当然,

文章中的最后一个例子非常接近我所需要的但不是
工作(当我运行它的时候我一直在08/11/0287)。


我的目标是有一种方法可以判断它是否是一个月的特定日期

所以我可以向用户显示一条消息。我只是无法得到我的头

在必要的日期数学周围。


非常感谢任何帮助。谢谢


- Andrew
Having trouble with some date computations. I need to get the date of a
specific day within a month - the 1st Thursday or the 2nd Monday, etc. I
found an article ( http://www.dotnetspider.com/kb/Article1252.aspx) that
gives examples using on the DateAdd() and DateDiff() functions, but it
seems that not all of the examples work as described.... and of course,
the last example in the article is very close to what I need yet does not
work (I keep getting 08/11/0287 when I run it).

My goal is to have a way I can tell if it is a specific day of the month
so I can display a message to the user. I am just not able to get my head
around the necessary date math.

Any help is greatly appreciated. Thanks

-- Andrew



我用PHP编写了一个功能齐全的日历应用程序,我可以给你一些

我用于此类事情的逻辑概念:


1)任何一天的第一天---星期一,星期二或其他 - - 在给定的

月份必须介于1和7之间(含)。


2)同样,第二个必须介于8和14之间,

包含。所以你可以做两个测试:1)给定的日期是星期二(或

无论如何),2)它是在一周的给定日期范围之间你是

正在寻找?


例如:美国的感恩节是在11月的第四个星期四,

这意味着它必须1)成为一个星期四和2)在11月22日至28日期间在
范围内。对于给定的年份,总有一个且仅有一个日期

满足这些标准。因此,只需迭代22-28,看看哪一个

出现在星期四。


3)本月某一天的_last_取决于月......如果

月份有31天,以及24日到30日之间,那么
必须介于当月25日和31日之间(含)如果

月有30天。您可以使用此逻辑来计算阵亡将士纪念日等。


4)您可以将此原则扩展到更复杂的模式,例如

选举一天(这是11月第一个星期一之后的第一个星期二)。使用这种方法,只需测试11月2日至8日的日期范围,看看

哪一个是星期二。


这是否能让你站稳脚跟你需要做什么?

I wrote a full-featured calendar application in PHP, and I can give you some
conceptual ideas for the logic I used for this kind of thing:

1) The first of any day --- Monday, Tuesday, or whatever --- in a given
month must fall between the 1st and 7th, inclusive.

2) Likewise, the second one must fall between the 8th and the 14th,
inclusive. So you can just do two tests: 1) is the given date a Tuesday (or
whatever), and 2) is it between the given date range for the week you''re
looking for?

Example: Thanksgiving in the US falls on the fourth Thursday of November,
which means that it must 1) be a Thursday and also 2) be in the range of
Nov. 22-28. For a given year, there is always one and only one date that
satisfies these criteria. So just iterate through 22-28 and see which one
comes up as Thursday.

3) The _last_ of a day in the month changes depending on the month... it
must fall between the 25th and the 31st of the month (inclusive) if the
month has 31 days, and between the 24th and the 30th (inclusive) if the
month has 30 days. You could use this logic to figure out Memorial Day, etc.

4) You can extend this principle to more complex patterns, such as that for
Election Day (which is the first Tuesday after the first Monday of
November). Using this method, simply test the range of dates Nov. 2-8 to see
which one is a Tuesday.

Does that give you a foothold on what you need to do?


尼斯写的彼得,


如果这是作业,那么它非常适合我们希望在这个地方提供比这个新闻组更好的信息。


Cor


" Peter" ; < st ***** @ hotmail.comschreef in bericht

news:************** @ newsread3.news.pas.earthlink .net。 ..
Nice written Peter,

If this is homework, then it fits exact in the place as we like to give
information than in this newsgroup.

Cor

"Peter" <st*****@hotmail.comschreef in bericht
news:At**************@newsread3.news.pas.earthlink .net...

" Andrew" < An **** @ somewhereoutthere.com写在留言中

news:uH ************** @ TK2MSFTNGP03.phx.gbl ...
"Andrew" <An****@somewhereoutthere.comwrote in message
news:uH**************@TK2MSFTNGP03.phx.gbl...

>有些日期计算有问题。我需要在一个月内获得特定日期的日期 - 第一个星期四或第二个星期一等。我发现了一篇文章( http://www.dotnetspider.com/kb/Article1252.aspx
给出了在DateAdd上使用的示例()和DateDiff()函数,但似乎并非所有示例都按照描述工作....当然,
文章中的最后一个示例非常接近我需要的内容没有工作(当我运行它的时候我一直在08/11/0287)。

我的目标是有一种方法可以判断它是否是一个月的特定日期
所以我可以向用户显示一条消息。我只是无法在必要的日期数学周围得到我的


任何帮助非常感谢。谢谢

- Andrew
>Having trouble with some date computations. I need to get the date of a
specific day within a month - the 1st Thursday or the 2nd Monday, etc. I
found an article ( http://www.dotnetspider.com/kb/Article1252.aspx) that
gives examples using on the DateAdd() and DateDiff() functions, but it
seems that not all of the examples work as described.... and of course,
the last example in the article is very close to what I need yet does not
work (I keep getting 08/11/0287 when I run it).

My goal is to have a way I can tell if it is a specific day of the month
so I can display a message to the user. I am just not able to get my
head around the necessary date math.

Any help is greatly appreciated. Thanks

-- Andrew



我用PHP编写了一个功能齐全的日历应用程序,我可以给你

对于我用于此类事情的逻辑的一些概念性想法:


1)任何一天的第一天---周一,周二或其他任何一个

月必须在1到7之间,包括在内。


2)同样,第二个必须介于8和14之间,
包括在内。所以你可以做两个测试:1)给定的日期是星期二

(或其他),2)它是在一周的给定日期范围之间

你在寻找吗?


例子:美国的感恩节是在11月的第四个星期四,

这意味着它必须是1)星期四和2)在11月22日至28日期间在
范围内。对于给定的年份,总有一个且仅有一个日期

满足这些标准。因此,只需迭代22-28,看看哪一个

出现在星期四。


3)本月某一天的_last_取决于月......如果

月份有31天,以及24日到30日之间,那么
必须介于当月25日和31日之间(含)如果

月有30天。您可以使用此逻辑来计算阵亡将士纪念日,

等等。您可以将此原则扩展到更复杂的模式,例如

选举日(这是11月第一个星期一之后的第一个星期二)。使用这种方法,只需测试11月2日至8日的日期范围至

查看哪一个是星期二。


这是否能让您立足于你需要做什么?


I wrote a full-featured calendar application in PHP, and I can give you
some conceptual ideas for the logic I used for this kind of thing:

1) The first of any day --- Monday, Tuesday, or whatever --- in a given
month must fall between the 1st and 7th, inclusive.

2) Likewise, the second one must fall between the 8th and the 14th,
inclusive. So you can just do two tests: 1) is the given date a Tuesday
(or whatever), and 2) is it between the given date range for the week
you''re looking for?

Example: Thanksgiving in the US falls on the fourth Thursday of November,
which means that it must 1) be a Thursday and also 2) be in the range of
Nov. 22-28. For a given year, there is always one and only one date that
satisfies these criteria. So just iterate through 22-28 and see which one
comes up as Thursday.

3) The _last_ of a day in the month changes depending on the month... it
must fall between the 25th and the 31st of the month (inclusive) if the
month has 31 days, and between the 24th and the 30th (inclusive) if the
month has 30 days. You could use this logic to figure out Memorial Day,
etc.

4) You can extend this principle to more complex patterns, such as that
for Election Day (which is the first Tuesday after the first Monday of
November). Using this method, simply test the range of dates Nov. 2-8 to
see which one is a Tuesday.

Does that give you a foothold on what you need to do?



Peter写道:
Peter wrote:

示例:美国的感恩节落在11月的第四个星期四

,这意味着它必须是1)星期四,并且2)在11月22日至28日的范围内。

。对于给定的一年,总有一个且仅有一个满足这些标准的日期。因此,只需迭代22-28

,看看哪一个出现在星期四。


3)本月一天的_last_取决于月......如果

月份有31天,以及24日到30日之间,那么
必须介于当月25日和31日之间(含)如果

月有30天。您可以使用此逻辑来计算阵亡将士纪念日,

等等。您可以将此原则扩展到更复杂的模式,例如

选举日(这是11月第一个星期一之后的第一个星期二)。使用这种方法,只需测试11月2日至8日的日期范围,即

查看哪一个是星期二。
Example: Thanksgiving in the US falls on the fourth Thursday of
November, which means that it must 1) be a Thursday and also 2) be in
the range of Nov. 22-28. For a given year, there is always one and only
one date that satisfies these criteria. So just iterate through 22-28
and see which one comes up as Thursday.

3) The _last_ of a day in the month changes depending on the month... it
must fall between the 25th and the 31st of the month (inclusive) if the
month has 31 days, and between the 24th and the 30th (inclusive) if the
month has 30 days. You could use this logic to figure out Memorial Day,
etc.

4) You can extend this principle to more complex patterns, such as that
for Election Day (which is the first Tuesday after the first Monday of
November). Using this method, simply test the range of dates Nov. 2-8 to
see which one is a Tuesday.



我'' d总是计算当月第一天的工作日数,使用

作为偏移量。然后不需要迭代。


我也总是计算第一个星期一,星期二..添加7,14,21为

第二个,第三个,第四。


迭代正在尝试与一点点琐碎的数学计算。


-

问候

Matthias

I''d always calculate the weekday number of the first of the month, using
this as an offset. No iterating necessary then.

Also I''d always calculate the first Monday, Tuesday.. Adding 7,14,21 for
the second, third, fourth.

Iterating is trying versus a bit trivial math to calculate.

--
Greetings
Matthias


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

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