Click here to Skip to main content
Licence CPOL
First Posted 6 Jul 2009
Views 6,190
Downloads 34
Bookmarked 17 times

Time is of the essence

By | 6 Jul 2009 | Article
DateTime helper methods.

Introduction

While working in different projects, I encountered frustrating problems related to date and time programming. So I wrote a few classes that helped me tackle those problems.

The code

Here is my static helper class:

public static DayOfWeek GetLastDayOfWeek(CultureInfo culture)
public static DateTime GetFirstDateOfWeek(DateTime dateTime, CultureInfo culture)
public static DateTime GetLastDateOfWeek(DateTime dateTime, CultureInfo culture)
public static DateTime GetLastDateOfMonth(int year, int month)
public static DateTime GetLastDateOfYear(int year)
public static int GetWeeksInYear(int year, CultureInfo culture)
public static int GetRemainingDaysInWeek(DateTime dateTime, CultureInfo culture)
public static int GetWeekNumber(DateTime dateTime, CultureInfo culture)
public static bool AreTimePeriodsIntersecting(TimePeriod tp1, TimePeriod tp2)
public static bool IsDateInPeriod(DateTime dateTime, TimePeriod timePeriod)

The TimeDifference class receives a start date and an end date, and returns the difference in Years, Months, Weeks, Days, Hours, Minutes, Seconds, and Miliseconds between them in two modes.

Let's take, for example, two dates: 06/26/2009 and 05/02/2013.

  • First difference mode (time-units independence): 4 years, or (more accurately) 47 months, or (more accurately) 200 weeks, or (more accurately) 1406 days.
  • Second difference mode (time-units dependence): 3 years and 10 months and 6 days.

Hope it will be of great help to someone, and please report any errors or suggestions.

NB: TimeZone and Daylight Savings Time are not taken into account.

License

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

About the Author

ovidiu_tudorache

Software Developer (Senior)

Romania Romania

Member



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
QuestionGetRemainingDaysInWeek PinmemberDoncp13:41 14 Jul '09  
GeneralWeekNumber PinmemberJiaozi21:19 6 Jul '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 6 Jul 2009
Article Copyright 2009 by ovidiu_tudorache
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid