DateTimeOffset - SetTime
Sets the time of the current date with minute precision.
public static void Main() { DateTimeOffset date = new DateTimeOffset(2007, 10, 1, 8, 45, 0, new TimeSpan(5, 0, 0)); Console.WriteLine("Current Date : {0}", date); // C# Extension Method: DateTimeOffset - SetTime DateTimeOffset newDate = date.SetTime(23,19,57); Console.WriteLine("After calling SetTime: {0}", newDate); }
View Source
using System; public static partial class Extensions { /// <summary> /// Sets the time of the current date with minute precision. /// </summary> /// <param name="current">The current date.</param> /// <param name="hour">The hour.</param> /// <returns>A DateTimeOffset.</returns> public static DateTimeOffset SetTime(this DateTimeOffset current, int hour) { return SetTime(current, hour, 0, 0, 0); } /// <summary> /// Sets the time of the current date with minute precision. /// </summary> /// <param name="current">The current date.</param> /// <param name="hour">The hour.</param> /// <param name="minute">The minute.</param> /// <returns>A DateTimeOffset.</returns> public static DateTimeOffset SetTime(this DateTimeOffset current, int hour, int minute) { return SetTime(current, hour, minute, 0, 0); } /// <summary> /// Sets the time of the current date with second precision. /// </summary> /// <param name="current">The current date.</param> /// <param name="hour">The hour.</param> /// <param name="minute">The minute.</param> /// <param name="second">The second.</param> /// <returns>A DateTimeOffset.</returns> public static DateTimeOffset SetTime(this DateTimeOffset current, int hour, int minute, int second) { return SetTime(current, hour, minute, second, 0); } /// <summary> /// Sets the time of the current date with millisecond precision. /// </summary> /// <param name="current">The current date.</param> /// <param name="hour">The hour.</param> /// <param name="minute">The minute.</param> /// <param name="second">The second.</param> /// <param name="millisecond">The millisecond.</param> /// <returns>A DateTimeOffset.</returns> public static DateTimeOffset SetTime(this DateTimeOffset current, int hour, int minute, int second, int millisecond) { return new DateTime(current.Year, current.Month, current.Day, hour, minute, second, millisecond); } }