DateTime.Today Example
This page was last reviewed on Jun 23, 2022.
Dot Net Perls
DateTime.Today. This returns just the day—without the time. This is different from DateTime.Now, which returns as much information as it can.
The Today property returns a DateTime struct with the hour, minutes and seconds set to zero. Today is the same as the Now property. But it has no time part.
This program gets the current day (with Today) and the current time (with Now). The program was executed in the early afternoon, but Today is still set to midnight.
Tip This program was run on a specific date. When you run it on your computer, the time will differ.
using System; class Program { static void Main() { DateTime today = DateTime.Today; DateTime now = DateTime.Now; Console.WriteLine(today); Console.WriteLine(now); } }
6/23/2022 12:00:00 AM 6/23/2022 6:23:42 AM
Usage. Today is useful if you need to determine whether a piece of data was updated today or not. In this case, you can test the data against DateTime.Today.
A review. We used the DateTime.Today property, which has an important difference from DateTime.Now. It contains no values other than the current day.
This makes it useful for sorting and filtering tasks, or when you simply don't need the time. You can get DateTime.Today from DateTime.Now on your own—remove the hours, minutes and seconds.
Sort DateTime List
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 23, 2022 (new example).
© 2007-2024 Sam Allen.