diwatu
diwatu

Reputation: 5699

Is there a better way to truncate DateTime in C#?

Assume I have a DateTime:

    DateTime datetime(2013,11,08,17,45,23,300);//2013:11:08 17:45:23:300

I want to truncate this DateTime by differenct accuracy and return the minimum DateTime like this:

    Year:         2013:01:01 00:00:00:000      
    Quarter:      2013:10:01 00:00:00:000    //Oct is first month of that quarter 
    Month:        2013:11:01 00:00:00:000
    Week:         2013:11:03 00:00:00:000    // 3rd is Sunday of that week
    Day:          2013:11:08 00:00:00:000
    Hours         2013:11:08 17:00:00:000   
    Minute:       2013:11:08 17:45:00:000   
    Second:       2013:11:08 17:45:23:000   

I know you can do it by changing different part of the DateTime, is there a better way to do it? or is there already a build in function in .net which I don't know?

Upvotes: 4

Views: 1567

Answers (3)

Xinbi
Xinbi

Reputation: 272

There's not one that I know of, but this should do the trick:

public enum Accuracy { Year, Quarter, Month, Week, Day, Hour, Minute, Second};

private static DateTime TruncateDate(DateTime inDate, Accuracy accuracy){
        switch (accuracy)
        {
            case Accuracy.Year:
                return new DateTime(inDate.Year, 1, 1);
            case Accuracy.Quarter:
                int i = inDate.Month % 3;
                return new DateTime(inDate.Year, inDate.Month - i + 1, 1);
            case Accuracy.Month:
                return new DateTime(inDate.Year, inDate.Month, 1);
            case Accuracy.Week:
                return new DateTime(inDate.Year, inDate.Month, inDate.Day).AddDays(-(int)inDate.DayOfWeek);
            case Accuracy.Day:
                return new DateTime(inDate.Year, inDate.Month, inDate.Day);
            case Accuracy.Hour:
                return new DateTime(inDate.Year, inDate.Month, inDate.Day, inDate.Hour, 0, 0);
            case Accuracy.Minute:
                return new DateTime(inDate.Year, inDate.Month, inDate.Day, inDate.Hour, inDate.Minute, 0);
            case Accuracy.Second:
                return new DateTime(inDate.Year, inDate.Month, inDate.Day, inDate.Hour, inDate.Minute, inDate.Second);
            default:
                throw new ArgumentOutOfRangeException("accuracy");
        }
    }

Edit: corrected for when Sunday is in a different month.

Upvotes: 4

Jeppe Stig Nielsen
Jeppe Stig Nielsen

Reputation: 61982

Based on my own comment:

public static DateTime Truncate(this DateTime dt, TimeSpan resolution)
{
  return new DateTime(dt.Ticks - dt.Ticks % resolution.Ticks);
}

Edit:

Example use: var truncated = dt.Truncate(Timespan.FromHours(1.0));

As I said in that comment, this is not useful for stuff like years and quarters of a year whose lengths (mathematically) vary from instance to instance.

Upvotes: 3

SlaterCodes
SlaterCodes

Reputation: 1139

This creates a DateTime at midnight of Today. You can take any DateTime and call the 'Date' property to get rid of hours/minutes/seconds.

// 1/7/2014 12:00:00 AM
DateTime.Now.Date

From there to get a specific time like 5AM

var 5amDate = DateTime.Now.Date.AddHours(5)
// 1/7/2014 5:00:00 AM

Upvotes: 0

Related Questions