To Nearest Minute - CSharp System

CSharp examples for System:DateTime Minute

Description

To Nearest Minute

Demo Code


using System;//from ww  w  . j a v  a  2  s  . c om

public class Main{
        public static DateTime ToNearestMinute(this DateTime dateTime)
        {
            return dateTime.Truncate(TimeSpan.FromMinutes(1));
        }
        private static DateTime Truncate(this DateTime dateTime, TimeSpan timeSpan)
        {
            if (timeSpan == TimeSpan.Zero)
            {
                return dateTime;
            }

            return dateTime.AddTicks(-(dateTime.Ticks%timeSpan.Ticks));
        }
}

Related Tutorials