计算日期差异 [英] Calculate difference in dates

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

问题描述

对于我的网站,我想显示我儿子的年龄,

个月,几天和几个小时。


现在我设法得到总数的结果。就像

天的总数一样。


这是开头:


starttime = Date.parse( 8月10,2003,07:07)

sdt =新日期(开始时间)

starttime = Math.ceil((starttime)/ 1000/60/60 / 24)

ndt =新日期()


y = sdt.getYear()

m = sdt.getMonth()+ 1

d = sdt.getDate()

h = starttime


Thanx

For my website i would like to display the age of my son in years,
months, days and hours.

For now i manage to get a result for totals. Like the total number of
days.

This is the beginning:

starttime = Date.parse("Aug 10,2003, 07:07")
sdt = new Date(starttime)
starttime= Math.ceil((starttime) / 1000 / 60 / 60 / 24 )
ndt = new Date()

y = sdt.getYear()
m = sdt.getMonth() + 1
d = sdt.getDate()
h = starttime

Thanx

推荐答案

Frank于2004年8月29日在comp.lang.javascript中写道
Frank wrote on 29 aug 2004 in comp.lang.javascript:
对于我的网站,我想显示多年来我儿子的年龄,几个月,几天和几个小时。

现在我设法得到总数的结果。就像
天的总数一样。

这是开始:

starttime = Date.parse(" Aug 10,2003,07:07")
sdt =新日期(开始时间)
starttime = Math.ceil((starttime)/ 1000/60/60/24)
ndt = new Date()
y = sdt.getYear()
m = sdt.getMonth()+ 1
d = sdt.getDate()
h = starttime
For my website i would like to display the age of my son in years,
months, days and hours.

For now i manage to get a result for totals. Like the total number of
days.

This is the beginning:

starttime = Date.parse("Aug 10,2003, 07:07")
sdt = new Date(starttime)
starttime= Math.ceil((starttime) / 1000 / 60 / 60 / 24 )
ndt = new Date()

y = sdt.getYear()
m = sdt.getMonth() + 1
d = sdt.getDate()
h = starttime



你的儿子有点年轻的网络; - }


阅读常见问题:


< http:/ /www.merlyn.demon.co.uk/js-date0.htm#DC>



< http://www.merlyn.demon.co。 uk / js-date1.htm #diff>

-

Evertjan。

荷兰。

(请将x''es更改为我的电子邮件地址中的点数,

但请让我们继续讨论新闻组。



A bit young for your son toread the web ;-}

Read the faq:

<http://www.merlyn.demon.co.uk/js-date0.htm#DC>
and
<http://www.merlyn.demon.co.uk/js-date1.htm#diff>
--
Evertjan.
The Netherlands.
(Please change the x''es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)


2004年8月29日08:18:46 -0700,Frank< fa ***** @ hotmail.com>写道:
On 29 Aug 2004 08:18:46 -0700, Frank <fa*****@hotmail.com> wrote:
对于我的网站,我想显示我儿子的年龄,几个月,几天,几个小时。

目前我设法获得总数的结果。就像
天的总数一样。

这是开始:

starttime = Date.parse(" Aug 10,2003,07:07")
For my website i would like to display the age of my son in years,
months, days and hours.

For now i manage to get a result for totals. Like the total number of
days.

This is the beginning:

starttime = Date.parse("Aug 10,2003, 07:07")




使用数字而不是字符串会更安全,因为字符串日期格式没有确切的定义。我确定上面的格式

会导致某些浏览器出现问题。


var birth = new Date(2003,7,10,7, 7);


[snip]


计算日期差异只需减去一个

日期另一个人的对象。这将产生一个代表

毫秒的数字。然后,您可以使用新的Date对象返回

年,月,日和小时的数量:


var difference = new Date(new Date( ) - 出生),

years = difference.getFullYear() - 1970,

months = difference.getMonth(),

days = difference .getDate() - 1,

hours = difference.getHours(),

t = new String(years);


上述调整基于以下事实:零(0)

的时间表示日期,格林威治标准时间01-Jan-1970 00:00:00。调整使得

各自的值从零开始。


t + =''year'';

if(1! =年){t + ='s'';;}

t + ='',''+月+''月'';

if(1 = =月){t + ='s'';;}

t + ='',''+ days +''day'';

if(1 = =天){t + ='s'';;}

t + ='',''+小时+''小时'';

if(1 ==小时){t + ='s'';;}

alert(t);


希望有所帮助,

Mike


-

Michael Winter

替换.invalid与.uk通过电子邮件回复。



It would be safer to use numbers, rather than a string as there is no
exact definition for string date formats. I''m certain that the format
above will cause problems in some browsers.

var birth = new Date(2003, 7, 10, 7, 7);

[snip]

Calculating differences in dates is simply a matter of subtracting one
Date object from another. This will yield a number representing the
milliseconds. You can then use a new Date object to return the number of
years, months, days and hours:

var difference = new Date(new Date() - birth),
years = difference.getFullYear() - 1970,
months = difference.getMonth(),
days = difference.getDate() - 1,
hours = difference.getHours(),
t = new String(years);

The adjustments above are based on the fact that a time of zero (0)
represents the date, 01-Jan-1970 00:00:00 GMT. The adjustments make the
respective values zero-based.

t += '' year'';
if(1 != years) {t += ''s'';}
t += '', '' + months + '' month'';
if(1 == months) {t += ''s'';}
t += '', '' + days + '' day'';
if(1 == days) {t += ''s'';}
t += '', and '' + hours + '' hour'';
if(1 == hours) {t += ''s'';}
alert(t);

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.


JRS:文章< opsdh37io1x13kvk @ atlantis>,日期为2004年8月29日星期日

16:01 :43,见于新闻:comp.lang.javascript,Michael Winter< M.Winter@bl

ueyonder.co.invalid>发表:
JRS: In article <opsdh37io1x13kvk@atlantis>, dated Sun, 29 Aug 2004
16:01:43, seen in news:comp.lang.javascript, Michael Winter <M.Winter@bl
ueyonder.co.invalid> posted :
2004年8月29日08:18:46 -0700,Frank< fa ***** @ hotmail.com>写道:
On 29 Aug 2004 08:18:46 -0700, Frank <fa*****@hotmail.com> wrote:
对于我的网站,我想显示我儿子的年龄,几个月,几天,几个小时。

目前我设法获得总数的结果。就像
天的总数一样。

这是开始:

starttime = Date.parse(" Aug 10,2003,07:07")
使用数字而不是字符串会更安全,因为字符串日期格式没有确切的定义。我确定上面的格式会导致某些浏览器出现问题。
For my website i would like to display the age of my son in years,
months, days and hours.

For now i manage to get a result for totals. Like the total number of
days.

This is the beginning:

starttime = Date.parse("Aug 10,2003, 07:07")
It would be safer to use numbers, rather than a string as there is no
exact definition for string date formats. I''m certain that the format
above will cause problems in some browsers.




任何人都可以提供一个真实的失败示例吗? ISTM值得解决

是否所有javascript系统,无论如何配置,都可以

以任意顺序读取英语-MON DD YYYY的日期和

合理的标点符号。

var birth = new Date(2003,7,10,7,7);


不完全相同;第一个返回time_t(ms),

秒返回一个Date对象。


该表单使用Month-1,因此适合人为错误;我将

建议新的日期(2003/08/10 07:07),其中字符串看似

正确的日期并且近在咫尺我的系统接受ISO-8601。再一次,可以

任何人找到该表格失败的例子吗?


[snip]

计算日期的差异只是简单的从另一个中减去一个
Date对象的问题。


编号确实给出了绝对时间间隔。

这将产生一个代表
毫秒的数字。


是。

然后您可以使用新的Date对象返回
年,月,日和小时的数量:


否; * a *数量......


考虑一个出生的男孩2004-01-01 00:00:00;在2008-12-31 12:00:00,他将近5岁,并期待他的派对在第二天。

他将是1461 + 365.5天。


考虑一个出生于1970-01-01 00:00:00的男孩;他将是1461 + 365.5天

岁的1975-01-01 12:00:00他将是5岁,期待

到他的派对上下午。

2004-02-01出生的男孩将在2004-03-01一个月大; 29天。

2005-02-01出生的男孩将在2005-03-01一个月大; 28天。

从1970-01-01开始,29/28天还不到一个月。


小伙子在2004年6个月大了 - 02-10;


starttime = Date.parse(" Aug 10,2003,07:07")

finaltime = Date.parse(" 2004年2月10日,07:07


D =新日期(最终时间 - 开始时间)


给了我7月4日星期六02: 00:00 UTC + 0100 1970 - > 6 mo 3 dy 2 hr;

每年都相同,因为2月底的实时时间不会越过。

现在考虑一下3月28日中午出生的所有年龄段的人。

他们将在接下来的4月4日中午出生一周。但是欧洲的那些
[〜]其中3/7将比其他4/7的小b / b小一小时;可能/反之亦然/在美国大部分地区。


[〜] EU&邻居;但不是冰岛(可能还有其他例外)。


此外,小伙子可能是外国人。与格林尼治标准时间的差异将在确定间隔时适当地允许(bn)
;但数据是格林威治标准时间1970.0格林威治标准时间。你的方法,将diff添加到datum,然后使用

getFullYear系列,根据

应答系统的位置给出结果。很少有地方全年使用GMT作为民用时间。

t + =''year'';
if(1!= years){t + =''s'';}
t + ='',''+月+''月'';
if(1 == months){t + ='s'';;}
t + =' ',''+天+''天'';
如果(1 ==天){t + =''s'';}}
t + =''和''+小时+''小时';
如果(1 ==小时){t + =''s'';}
alert(t);



Can anyone provide an actual example of failure? ISTM worth settling
the point of whether all javascript systems, however configured, can
read a date with English-MON DD YYYY in arbitrary order and
reasonable punctuation.
var birth = new Date(2003, 7, 10, 7, 7);
Not fully equivalent, though; the first returns a time_t (ms) and the
second a Date Object.

That form uses Month-1, and hence is amenable to human error; I''d
suggest new Date("2003/08/10 07:07") , in which the string looks like
the right date and is as near ISO-8601 as my system accepts. Again, can
anyone find an example of failure of that form?

[snip]

Calculating differences in dates is simply a matter of subtracting one
Date object from another.
No. It does give the absolute time interval.
This will yield a number representing the
milliseconds.
Yes.
You can then use a new Date object to return the number of
years, months, days and hours:
No; *a* number of ...

Consider a boy born 2004-01-01 00:00:00; at 2008-12-31 12:00:00 he will
be nearly 5 years old, and looking forward to his party on the next day.
He will be 1461 + 365.5 days old.

Consider a boy born 1970-01-01 00:00:00; he will be 1461 + 365.5 days
old on 1975-01-01 12:00:00 he will be 5 years old, and looking forward
to his party on that afternoon.
A boy born 2004-02-01 will be a month old on 2004-03-01 ; 29 days.
A boy born 2005-02-01 will be a month old on 2005-03-01 ; 28 days.
Starting from 1970-01-01, 29/28 days is not yet a month.

The lad was 6 months old on 2004-02-10;

starttime = Date.parse("Aug 10,2003, 07:07")
finaltime = Date.parse("Feb 10,2004, 07:07")

D = new Date(finaltime-starttime)

gives me Sat Jul 4 02:00:00 UTC+0100 1970 -> 6 mo 3 dy 2 hr; the
same every year, since the end of Feb real time is not crossed.
Now consider those, of all ages, who were born on March 28th at noon.
They will have been a week old on the following April 4th, at noon. But
in Europe[~] 3/7 of them will then have been an hour younger than the
other 4/7; and probably /vice versa/ in much of the USA.

[~] EU & neighbours; but not Iceland (there may be other exceptions).

Moreover, the lad may be a foreigner. Differences from GMT will be
(too) properly allowed for in determining the interval; but datum is
1970.0 GMT. Your method, which adds diff to datum and then uses the
getFullYear family, gives a result depending on the location of the
answering system. Few places use GMT as civil time year-round.
t += '' year'';
if(1 != years) {t += ''s'';}
t += '', '' + months + '' month'';
if(1 == months) {t += ''s'';}
t += '', '' + days + '' day'';
if(1 == days) {t += ''s'';}
t += '', and '' + hours + '' hour'';
if(1 == hours) {t += ''s'';}
alert(t);




错误的多元化?


恕我直言,OP需要在整个民用年份从DoB

前进,直到下一步将通过现在一瞬间,数着他们;然后

同样在几个月,几天,几小时和几分钟内。


他可以倒退;我希望答案有时会[*]

不同。

[*]假设OP重复测试统计 -

重要的[+]儿子数量[#]。

[+]这不一定是他自己的。

[#]或女儿,妻子等等。 />
如果

小伙子是韩语(如果半韩语为4.5个月),则需要进一步修正,大约9个月,并且所述日期为DoB。

见下文。


-

?约翰斯托克顿,英国萨里。 ?@merlyn.demon.co.uk Turnpike v4.00 IE 4?

< URL:http://www.jibbering.com/faq/> JL / RC:新闻常见问题:comp.lang.javascript

< URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr数学,日期,来源。

< URL:http://www.merlyn.demon.co.uk/> TP / BP / Delphi / jscr /& c,常见问题项目,链接。



Erroneous pluralisation?

IMHO, the OP needs to step forwards in integer civil years from the DoB
until the next step will pass the present instant, counting them; then
likewise in months, days, hours, and minutes.

He could step backwards; I expect that the answer will sometimes[*]
differ.
[*] Assuming that the OP repeats the test for a statistically-
significant[+] number of sons[#].
[+] Which need not be all his own.
[#] Or daughters, wives, etc.
A further correction, probably of about 9 months, will be needed if the
lad is Korean (4.5 mo if half-Korean?), and the stated date is DoB.
See via below.

--
? John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ?
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.


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

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