Convert To Utc From Time Zone - CSharp System

CSharp examples for System:DateTime Timezone

Description

Convert To Utc From Time Zone

Demo Code


using System;/*  w ww .ja  v a  2s  .c o  m*/
using NodaTime;

public class Main{
        public static DateTime ConvertToUtcFromTimeZone(this DateTime dateTime, DateTimeZone timeZone)
        {
            timeZone.ThrowIfNull("timeZone");

            if (dateTime.Kind == DateTimeKind.Utc)
                return dateTime;

            return timeZone.AtLeniently(LocalDateTime.FromDateTime(dateTime)).WithZone(DateTimeZone.Utc).ToDateTimeUtc();
        }
        public static DateTime ConvertToUtcFromTimeZone(this DateTime dateTime, string timeZoneId)
        {
            timeZoneId.ThrowIfNull("timeZoneId");

            var timeZone = DateTimeZoneProviders.Tzdb[timeZoneId];
            return dateTime.ConvertToUtcFromTimeZone(timeZone);
        }
}

Related Tutorials