469,343 Members | 5,350 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,343 developers. It's quick & easy.

How to have DateTime to start at midnight???

Hi all

If the date and time on my computer is 23/02/06 15:43:20 and I do the
following:

DateTime dt;
dt = DateTime.today();

The variable 'dt' has the value '23/02/06 15:43:20'.
How can I make it so the time will always be 00:00:00. For example:

'dt' has the value '23/02/06 00:00:00'

Thanks in advance.

--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200602/1
Feb 23 '06 #1
7 36950
Hi Jon,

Try this:
DateTime dt = DateTime.Today().Date();

pagates

"Jon S via DotNetMonster.com" wrote:
Hi all

If the date and time on my computer is 23/02/06 15:43:20 and I do the
following:

DateTime dt;
dt = DateTime.today();

The variable 'dt' has the value '23/02/06 15:43:20'.
How can I make it so the time will always be 00:00:00. For example:

'dt' has the value '23/02/06 00:00:00'

Thanks in advance.

--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200602/1

Feb 23 '06 #2
Today does that. Are you not seeing same results?

DateTime dt = DateTime.Today;

Console.WriteLine(dt.ToString());
--
William Stacey [MVP]

"Jon S via DotNetMonster.com" <u2272@uwe> wrote in message
news:5c51f4881c338@uwe...
| Hi all
|
| If the date and time on my computer is 23/02/06 15:43:20 and I do the
| following:
|
| DateTime dt;
| dt = DateTime.today();
|
| The variable 'dt' has the value '23/02/06 15:43:20'.
| How can I make it so the time will always be 00:00:00. For example:
|
| 'dt' has the value '23/02/06 00:00:00'
|
| Thanks in advance.
|
| --
| Message posted via DotNetMonster.com
| http://www.dotnetmonster.com/Uwe/For...sharp/200602/1
Feb 23 '06 #3
On Thu, 23 Feb 2006 13:37:22 -0500, "William Stacey [MVP]"
<wi************@gmail.com> wrote:
Today does that. Are you not seeing same results?

DateTime dt = DateTime.Today;

Console.WriteLine(dt.ToString());

I've always done it with DateTime.Today. I wonder what's going on there?

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Feb 24 '06 #4
Jon S via DotNetMonster.com <u2272@uwe> wrote:
If the date and time on my computer is 23/02/06 15:43:20 and I do the
following:

DateTime dt;
dt = DateTime.today();

The variable 'dt' has the value '23/02/06 15:43:20'.
How can I make it so the time will always be 00:00:00. For example:

'dt' has the value '23/02/06 00:00:00'


I don't believe you're seeing what you think you're seeing. As other
posters have mentioned, DateTime.Today already sets the time to
midnight.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 24 '06 #5
Try something like :

DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month,
DateTime.Now.Day, 0, 0, 0);

Feb 27 '06 #6
Code Monkey wrote:
Try something like :

DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month,
DateTime.Now.Day, 0, 0, 0);


That's actually quite dangerous - because there's a possibility that
the date will change between those three invocations of DateTime.Now.

What's wrong with DateTime.Today though?

Jon

Feb 27 '06 #7
True.

How about:

DateTime dt1 = DateTime.Now;
DateTime dt = new DateTime(dt1.Year, dt1.Month, dt1.Day, 0, 0, 0);

One invocation of DateTime.Now.

Nothing wrong with DateTime.Today, guess it boils down to personal
preference - from MSDN "...A DateTime set to the date of this instance,
with the time part set to 00:00:00..."
(http://msdn.microsoft.com/library/de...odaytopic.asp).

Feb 27 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Joshua Beall | last post: by
2 posts views Thread by engsol | last post: by
12 posts views Thread by conckrish | last post: by
37 posts views Thread by ales | last post: by
1 post views Thread by =?Utf-8?B?RGFwcGVyRGFuSEBub3NwYW0ubm9zcGFt?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.