To Nearest Second - CSharp System

CSharp examples for System:DateTime Second

Description

To Nearest Second

Demo Code


using System;/*from   w w w . j  av a  2s  .c  om*/

public class Main{
        public static DateTime ToNearestSecond(this DateTime dateTime)
        {
            return dateTime.Truncate(TimeSpan.FromSeconds(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