Converts an existing to a but normalizes DateTimeOffset - CSharp System

CSharp examples for System:DateTimeOffset

Description

Converts an existing to a but normalizes DateTimeOffset

Demo Code


using System;/*from  w  w  w  .  j  a v  a  2  s. c  o  m*/

public class Main{
        public static DateTimeOffset ToDateTimeOffset(this DateTime dateTime, TimeSpan offset)
        {
            return new DateTimeOffset(DateTime.SpecifyKind(dateTime, DateTimeKind.Unspecified) , offset);
        }
        /// <summary>
        /// Converts an existing <see cref="DateTime"/> to a <see cref="DateTimeOffset"/> but normalizes the <see cref="DateTimeKind"/> 
        /// so that comparisons of converted <see cref="DateTime"/> instances retain the UTC/local agnostic behavior.
        /// </summary>
        public static DateTimeOffset ToDateTimeOffset(this DateTime dateTime)
        {
            return dateTime.ToDateTimeOffset(TimeSpan.Zero);
        }
}

Related Tutorials