How to have DateTime to start at midnight???

  • Thread starter Jon S via DotNetMonster.com
  • Start date
J

Jon S via DotNetMonster.com

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.
 
W

William Stacey [MVP]

Today does that. Are you not seeing same results?

DateTime dt = DateTime.Today;

Console.WriteLine(dt.ToString());


--
William Stacey [MVP]

| 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/Forums.aspx/dotnet-csharp/200602/1
 
J

Jon Skeet [C# MVP]

Jon S via DotNetMonster.com said:
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.
 
C

Code Monkey

Try something like :

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

Jon Skeet [C# MVP]

Code said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top