473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 40837
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Joshua Beall | last post by:
Hi All, I am working on a mailing list program for a client, and I am wondering what tbe hest way to deal with script timeouts is. I realize that I could use set_time_limit() to increase the...
2
by: engsol | last post by:
I'm using Python to parse a bunch of s/w test files and make csv files for later report generation by MS ACCESS....(my boss loves the quick turn-around compared to C). Each log file may contain one...
2
by: Manfred Braun | last post by:
Hi All, I am on listing a class's fields with reflection and I have to handle fields which are array of type DateTime . I cannot get this to work and I get an unexpected run-time exception: ...
12
by: conckrish | last post by:
Hi all.. Can anyone tell me how to compare datetime objects?I ve three objects namely Current date,start date and end date.. I need to check the current date with Start date and end date....Plz...
37
by: ales | last post by:
Hello, I have a problem with creation of new thread. The method .Start() of newly created thread delays current thread for 0 - 1 second. Cpu while delay occurs is about 5%. Any idea? Here...
3
by: backroomboy | last post by:
how to activate program before the OS have started on turning on the computer
1
by: =?Utf-8?B?RGFwcGVyRGFuSEBub3NwYW0ubm9zcGFt?= | last post by:
Given the example below, can someone explain why TimeSpan.TotalDays gives a different result than subtracting 2 DateTime.ToOADates? I am completely stumped. Thanks in advance, Dan Example...
0
yasirmturk
by: yasirmturk | last post by:
Standard Date and Time Functions The essential date and time functions that every SQL Server database should have to ensure that you can easily manipulate dates and times without the need for any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.