关于DateTime的问题 [英] a question regarding DateTime

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

问题描述




..NET 2.0

我要为一个方法创建2个输入参数。这些是2个DateTime

参数 - 一个名为from的参数。另一个名为to...... - 来自和来回

日期时间值...


好​​的诀窍是来自 DateTime应该是这样的

01.< MM>。< YYYY ss:mm.hh这意味着今天是03.12.2007然后

" from ;日期应该是01.12.2007 00:00:00。什么是计算这个的最佳方式?


有什么建议吗?

解决方案

Jeff< do *** @ spam.mewrote:


.NET 2.0


我是即将为方法创建2个输入参数。这些是2个DateTime

参数 - 一个名为from的参数。另一个名为to...... - 来自和来回

日期时间值...


好​​的诀窍是来自 DateTime应该是这样的

01.< MM>。< YYYY ss:mm.hh这意味着今天是03.12.2007然后

" from ;日期应该是01.12.2007 00:00:00。计算这个的最佳方法是什么?b



DateTime值没有任何固有的格式 - 只有当你将
转换成字符串时才会转换它们''格式化。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

世界级。英国的NET培训: http://iterativetraining.co.uk


好的,我现在正在测试这个解决方案:


DateTime from

from = new DateTime(DateTime。 Now.Year,DateTime.Now.Month,1,0,0,0)


我正在调试代码,它似乎正在运行。你看到使用这段代码有什么问题吗?


Jeff< do *** @ spam.mewrote:
< blockquote class =post_quotes>
好​​的,我现在正在测试这个解决方案:


DateTime from

from = new DateTime(DateTime .Now.Year,DateTime.Now.Month,1,0,0,0)


我正在调试代码,它似乎正在运行。你看到使用这段代码有什么问题吗?



是 - 您正在调用DateTime.Now两次。我知道这不太可能,但

考虑的情况是12月31日,23:59:59.999 - 你打电话给

DateTime.Now和年份然后是时钟,然后你

调用DateTime.Now,月份是1月。现在你既没有Dec / /
2007也没有2008年1月 - 你有2007年1月。


现在使用


DateTime = DateTime.Now;

from = new DateTime(now.Year,now.Month,1,0,0,0)


(你确定吗)顺便说一下,你不想使用UTC吗?我个人

尽可能保持UTC,直到演示层,但我

don'我对你要做的事情已经足够了解了。只需

就可以考虑一下。)


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http: //www.msmvps.com/jon.skeet

英国的世界级.NET培训: http://iterativetraining.co.uk


Hey

..NET 2.0

I''m about to create 2 input parameters for a method. These are 2 DateTime
parameters - one named "from" and another named "to"... - from and to
DateTime values...

Okay the trick is that the "from" DateTime should be like this
01.<MM>.<YYYY ss:mm.hh which means that today is the 03.12.2007 then the
"from" date should be 01.12.2007 00:00:00. What is the best way of
calculating this?

any suggestions?

解决方案

Jeff <do***@spam.mewrote:

.NET 2.0

I''m about to create 2 input parameters for a method. These are 2 DateTime
parameters - one named "from" and another named "to"... - from and to
DateTime values...

Okay the trick is that the "from" DateTime should be like this
01.<MM>.<YYYY ss:mm.hh which means that today is the 03.12.2007 then the
"from" date should be 01.12.2007 00:00:00. What is the best way of
calculating this?

DateTime values don''t have any inherent formatting - it''s only when you
convert them to and from strings that they''re formatted.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Okay, I''ve testing this solution now:

DateTime from
from = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1, 0, 0, 0)

I''m debugging the code and it seems like it is working. Do you see any
problems using this code?.


Jeff <do***@spam.mewrote:

Okay, I''ve testing this solution now:

DateTime from
from = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1, 0, 0, 0)

I''m debugging the code and it seems like it is working. Do you see any
problems using this code?.

Yes - you''re calling DateTime.Now twice. I know it''s unlikely, but
consider the case where it''s December 31st, 23:59:59.999 - you call
DateTime.Now and the year is 2007. Then the clock times, and then you
call DateTime.Now and the month is January. Now you neither have Dec
2007 nor Jan 2008 - you have Jan 2007.

Use

DateTime now = DateTime.Now;
from = new DateTime(now.Year, now.Month, 1, 0, 0, 0)

(Are you sure you don''t want to be using UTC, by the way? I personally
try to keep to UTC whenever I can until the presentation layer, but I
don''t know enough about what you''re doing to say for sure. Just
something to think about.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


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

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