Click here to Skip to main content
Licence CPOL
First Posted 14 Nov 2006
Views 10,320
Bookmarked 8 times

DateTruncate

By | 14 Nov 2006 | Article
This article describes my DateTruncate function.

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

PIEBALDconsult

Software Developer (Senior)

United States United States

Member

BSCS 1992 Wentworth Institute of Technology
 
Originally from the Boston (MA) area. Lived in SoCal for a while. Now in the Phoenix (AZ) area.
 
OpenVMS enthusiast, ISO 8601 evangelist, photographer, opinionated SOB
 
---------------
 
"Typing is no substitute for thinking." -- R.W. Hamming
 
"I find it appalling that you can become a programmer with less training than it takes to become a plumber." -- Bjarne Stroustrup
 
ZagNut’s Law: Arrogance is inversely proportional to ability.
 
"Well blow me sideways with a plastic marionette. I've just learned something new - and if I could award you a 100 for that post I would. Way to go you keyboard lovegod you." -- Pete O'Hanlon
 
"linq'ish" sounds like "inept" in German -- Andreas Gieriet
 

"Things would be different if I ran the zoo." -- Dr. Seuss
 
"Wrong is evil, and it must be defeated." – Jeff Ello
 
"A good designer must rely on experience, on precise, logical thinking, and on pedantic exactness." -- Nigel Shaw
 

"Omit needless local variables." -- Strunk... had he taught programming
 
"DON'T BE LIBERAL IN WHAT YOU ACCEPT!"
 
"Software Engineers don't have Trophy Wives; they have Presentation Layers."
 
"We learn more from our mistakes than we do from getting it right the first time."
 
"I'm an old dog and I like old tricks."
 
"Sometimes the envelope pushes back and sometimes you get a really nasty paper cut."
 
"A method shall have one and only one return statement."
 
My first rule of debugging: "If you get a different error message, you're making progress."
 
My golden rule of database management: "Do not unto others' databases as you would not have done unto yours."
 
My general rule of software development: "Design should be top-down, but implementation should be bottom-up."
 
"Today's heresy is tomorrow's dogma."
or
"Today's dogma is yesterday's heresy."
 
"The registry is evil."
 
"Every tool is a hammer."

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 14 Nov 2006
Article Copyright 2006 by PIEBALDconsult
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid