CSharp examples for System:DateTime Timezone
Convert To Time Zone
using System;//from w w w . j av a 2 s . c om using NodaTime; public class Main{ public static DateTime ConvertToTimeZone(this DateTime dateTime, DateTimeZone timeZone) { timeZone.ThrowIfNull("timeZone"); if (dateTime.Kind != DateTimeKind.Utc) throw new InvalidOperationException("Source DateTime must be in UTC"); if (timeZone == DateTimeZone.Utc) return dateTime; return Instant.FromDateTimeUtc(dateTime).InZone(timeZone).ToDateTimeUnspecified(); } public static DateTime ConvertToTimeZone(this DateTime dateTime, string timeZoneId) { timeZoneId.ThrowIfNull("timeZoneId"); var timeZone = DateTimeZoneProviders.Tzdb[timeZoneId]; return dateTime.ConvertToTimeZone(timeZone); } }