Introduction
The method described in this article performs a variety of truncations on DateTime
values.
Using the code
LibTim.DateTruncate.cs contains two definitions: the DateTruncate
method and the TimeGranularity
enumeration. To use them, build them into your project and use something like:
System.DateTime d = PIEBALD.Lib.LibTim.DateTruncate ( System.DateTime.Now ,
PIEBALD.Lib.LibTim.TimeGranularity.Day ) ;
The DateTruncate.cs file contains a simple console program that uses the DateTruncate
method. If no parameter is provided, it uses System.DateTime.Now
:
C:\>DateTruncate
Year = 2006-01-01 00:00:00.00
Month = 2006-09-01 00:00:00.00
Day = 2006-09-15 00:00:00.00
Hour = 2006-09-15 11:00:00.00
Minute = 2006-09-15 11:49:00.00
Second = 2006-09-15 11:49:49.00
Quarter = 2006-07-01 00:00:00.00
Week = 2006-09-11 00:00:00.00
Sunday = 2006-09-10 00:00:00.00
Monday = 2006-09-11 00:00:00.00
Tuesday = 2006-09-12 00:00:00.00
Wednesday = 2006-09-13 00:00:00.00
Thursday = 2006-09-14 00:00:00.00
Friday = 2006-09-15 00:00:00.00
Saturday = 2006-09-09 00:00:00.00
Otherwise, it uses System.DateTime.Parse()
to attempt to parse the first parameter as a DateTime
:
C:\>DateTruncate "2007-01-01"
Year = 2007-01-01 00:00:00.00
Month = 2007-01-01 00:00:00.00
Day = 2007-01-01 00:00:00.00
Hour = 2007-01-01 00:00:00.00
Minute = 2007-01-01 00:00:00.00
Second = 2007-01-01 00:00:00.00
Quarter = 2007-01-01 00:00:00.00
Week = 2007-01-01 00:00:00.00
Sunday = 2006-12-31 00:00:00.00
Monday = 2007-01-01 00:00:00.00
Tuesday = 2006-12-26 00:00:00.00
Wednesday = 2006-12-27 00:00:00.00
Thursday = 2006-12-28 00:00:00.00
Friday = 2006-12-29 00:00:00.00
Saturday = 2006-12-30 00:00:00.00
(I chose 2007-01-01 in this example to show that the results using Year, Month, Day, Hour, Minute, Second, Quarter, Week, and Monday are all the same.)
History
- First uploaded - 2006-09-15.