DateTime数学 [英] DateTime Math

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

问题描述

任何人都可以想到一个简单的方法来获取两个DateTime值和

计算,双精度,半月(第一个和第十五个b / b)他们之间发生了支付期?


我开始手动为它编写一个功能,它只是变得更加丑陋和

uglier。而且我认为这是某人之前可能已经想到的
。任何想法?

问候,

David P. Donahue
dd ****** @ ccs.neu.edu
http: //www.cyber0ne.com

Can anyone think of an easy way to take two DateTime values and
calculate, with double precision, how many semi-monthly (the first and
the fifteenth) pay periods occur between them?

I started manually writing a function for it, and it just got uglier and
uglier. And I figure it''s something that someone has probably figured
out before. Any ideas?
Regards,
David P. Donahue
dd******@ccs.neu.edu
http://www.cyber0ne.com

推荐答案

不应该太难。当然,你首先使用DateTime.Subtract来获得
在两个日期之间获得TimeSpan。


棘手的部分是计算支付周期的分数发生。这个

很难,因为几个月的天数不一样。有些是b $ b $ 30,有些是31,2月和闰年是特殊情况。所以,你有

来确定你的规则,以确定什么是全薪

期间。它是两个平均长度的一半在

双精度浮点天?或者你根据每个月的前15天以及那个月的剩余天数

来逐月计算

?你如何计算这个取决于

这里的业务规则。


除了第一个月和最后一个月,它们之间的所有月份都将包含

正好是2.0个付款期,因为日历中的每个月都超过15天b $ b天,即使在闰年! ;-)所以,你唯一真正的任务就是从剩余时间中找出
。第一个

DateTime的月份之后的日子,以及第二个DateTime的月份之前的日子。

例如,如果开头和结束日期时间是2006年2月1日和5月28日,

2006,2个日期之间将有2个完整月份:3月和4月。

这2个月构成2 * 2.0D(4.0D)支付期。


因此,您需要确定开始和结束的每月日期

DateTimes。同样,根据您确定第二个支付期间长度的业务规则,其余部分将有所不同。例如,如果你确定一个支付期间平均构成15.345天(我在这个数字上做了
),你可以从第一个日期开始(2月1日) ,2006年)并从28(或29,取决于年份)减去1

,取结果,并除以

15.345。假设一个28天的月份,那就是27天/ 15.345 =

1.7595307917888563049853372434018支付期间,这加上4 =

5.759530791788856304985337243401支付期间,不包括结束。现在你

为第二个DateTime(2006年5月28日)做同样的事情。对于这个我们简单地

除以28.345 = 1.8246985988921472792440534376018支付期间,增加

到现有的5.759530791788856304985337243401支付期=

7.584229390681003584229390681002总支付期。


希望,如果您的业务规则不同,例如使用15作为

第一个支付期,以及第二个月的剩余天数,你b / b
应该能够应用我提供的相同的基本业务规则

开发你自己的算法。


-

HTH,


Kevin Spencer

Microsoft MVP

专业鸡肉沙拉炼金术士


大厚片由很多小薄片组成。


" David P. Donahue" <峰; dd ****** @ ccs.neu.edu>在留言中写道

新闻:%2 *************** @ TK2MSFTNGP05.phx.gbl ...
Shouldn''t be too hard. Of course, you begin by using DateTime.Subtract to
get the TimeSpan between the 2 dates.

The tricky part is calculating the fractions of pay periods that occur. This
is difficult because months are not the same number of days long. Some are
30, some are 31, and February and leap year are special cases. So, you have
to figure out your rules for determining what constitutes a "full pay
period." Is it exactly half of the average length of a month in
double-precision floating point days? Or do you calculate it on a
month-by-month basis, based upon the first 15 days of the month, and the
remaining number of days in that month? How you will calculate this depends
upon the business rule here.

Except for the first and last month, all months between them will contain
exactly 2.0 pay periods, since every month in the Calendar is more than 15
days long, even during leap year! ;-) So, your only real task is figuring
out the "left-overs," the days after the day of the month of the first
DateTime, and the days before the day of the month of the second DateTime.
For example, if the beginning and end DateTimes were Feb 1, 2006 and May 28,
2006, there will be 2 full months between the 2 Dates: March and April.
These 2 months constitute 2 * 2.0D (4.0D) pay periods.

So, you need to determine the Day of the month for the beginning and ending
DateTimes. Again, depending upon your business rule for determining the
length of the second pay period, the remainder will vary. If, for example,
you determine that a pay period constitues an average of 15.345 days (I made
up that number), you start with the first date (Feb 1, 2006) and subtract 1
from 28 (or 29, depending upon the year), take the result, and divide it by
15.345. Assuming a 28-day month, that leaves 27 days / 15.345 =
1.7595307917888563049853372434018 pay periods, which is added to 4 =
5.759530791788856304985337243401 pay periods, not counting the end. Now you
do the same for the second DateTime (May 28, 2006). For this we simply
divide 28 by 15.345 = 1.8246985988921472792440534376018 pay periods, added
to the existing 5.759530791788856304985337243401 pay periods =
7.584229390681003584229390681002 total pay periods.

Hopefully, if your business rule is different, such as using 15 for the
first pay period, and the remaining days in the month for the second, you
should be able to apply the same basic business rules I''ve provided to
develop your own algorithm.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Big thicks are made up of lots of little thins.

"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:%2***************@TK2MSFTNGP05.phx.gbl...
有谁能想到一个简单的方法来获取两个DateTime值并计算,双精度,半月(第一和第十五)
支付期间发生多少?

我开始手动为它编写一个函数,它变得更加丑陋和丑陋。我认为这是某人之前可能想到的东西。任何想法?

问候,
David P. Donahue
dd ** ****@ccs.neu.edu
http:// www。 cyber0ne.com
Can anyone think of an easy way to take two DateTime values and calculate,
with double precision, how many semi-monthly (the first and the fifteenth)
pay periods occur between them?

I started manually writing a function for it, and it just got uglier and
uglier. And I figure it''s something that someone has probably figured out
before. Any ideas?
Regards,
David P. Donahue
dd******@ccs.neu.edu
http://www.cyber0ne.com



>你将如何计算这取决于
> How you will calculate this depends
这里的业务规则。




我想那是'那里的满口。剩下的逻辑是

简单,并且会有所帮助。我想在这一点上我只需要

等到周一,并在确定支付期限时向客户询问更具体的业务

规则。是等式还是排除等式中的

开始/结束日期?它是由1-14,15 +还是由

1/2个月确定的?


至少,你给了我正确的问题,明天要问他们b $ b。谢谢!

问候,

David P. Donahue
dd******@ccs.neu.edu
http:/ /www.cyber0ne.com


如果我错了,请纠正我。一个月你支付两次。说4月17日至6月13日。

意味着四月你不会再付钱,可以支付2次,六月支付一次,

对吗?所以你基本上只需要知道月份的不同和

,无论第一个日期和最后一个日期是否超过15个或更少,那么应该是非常简单的。 br />

chanmm


" David P. Donahue" <峰; dd ****** @ ccs.neu.edu>在留言中写道

新闻:%2 *************** @ TK2MSFTNGP05.phx.gbl ...
Correct me if I am wrong. One month you pay twice. Say 17 April to 13 June.
Mean April you will not pay again, May pay 2 times and June pay 1 time,
right? So you basically only need to know the different of the month and
whether the first date and the last date is more than 15 or less then should
be pretty simple.

chanmm

"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:%2***************@TK2MSFTNGP05.phx.gbl...
有谁能想到一个简单的方法来获取两个DateTime值并计算,双精度,半月(第一和第十五)
支付期间发生多少?

我开始手动为它编写一个函数,它变得更加丑陋和丑陋。我认为这是某人之前可能想到的东西。任何想法?

问候,
David P. Donahue
dd ** ****@ccs.neu.edu
http:// www。 cyber0ne.com
Can anyone think of an easy way to take two DateTime values and calculate,
with double precision, how many semi-monthly (the first and the fifteenth)
pay periods occur between them?

I started manually writing a function for it, and it just got uglier and
uglier. And I figure it''s something that someone has probably figured out
before. Any ideas?
Regards,
David P. Donahue
dd******@ccs.neu.edu
http://www.cyber0ne.com



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

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