Victor's Blog

Live as if you were to die tomorrow. Learn as if you were to live forever

NAVIGATION - SEARCH

How to trim the datetime in c#?

Last week I need to write something to compare two datetime at the second level. So I have to trim the datetime part after second. There is the function I used to trim the datetime. 

 

public static DateTime TrimDate(DateTime date, long ticks)
{
    return new DateTime(date.Ticks - date.Ticks % ticks);
}

Examples about how to use the function

DateTime datNewDate = TrimDate(oldDate, TimeSpan.TicksPerSecond); 

Easy! 

 

blog comments powered by Disqus