Date handling in C#

To understand this, walk the code.
You can see it work at

using System;
public class Program
	public static void Main()
		Console.WriteLine("Local Server Time Zone:{0}",TimeZoneInfo.Local.BaseUtcOffset);
		string tzse = "Eastern Standard Time";
		string tzsw = "Pacific Standard Time";
		TimeZoneInfo tzie = TimeZoneInfo.FindSystemTimeZoneById(tzse);
		TimeZoneInfo tziw = TimeZoneInfo.FindSystemTimeZoneById(tzsw);

		TimeSpan tzo = tzie.BaseUtcOffset;
		Console.WriteLine("Base Time Zone East:{0}",tzo);

		TimeSpan tzo2e = tzie.GetUtcOffset(DateTime.UtcNow);
		Console.WriteLine("Adjusted Time Zone East:{0}",tzo2e);
		DateTime myTime = DateTime.Now;
		Console.WriteLine("My Time:{0}",myTime);
		DateTimeOffset submittedDate = new DateTimeOffset(myTime).ToOffset(tzo2e);
		Console.WriteLine("Just Change Offset from My Time East:{0}",submittedDate);

		DateTimeOffset myUtcTime = DateTimeOffset.UtcNow;
		Console.WriteLine("UTC Time:{0}",myUtcTime);
		Console.WriteLine("as DateTime:{0}",myUtcTime.DateTime);

		DateTimeOffset forcedDate = TimeZoneInfo.ConvertTimeFromUtc(myUtcTime.DateTime, tzie);
		Console.WriteLine("Use Convert East:{0}",forcedDate);

		DateTimeOffset adjustedDate = forcedDate.ToOffset(tzo2e);
		Console.WriteLine("Adjusted to Timezone East:{0}",adjustedDate);


		//Applied responses from
		//Sometimes I don't understand why my google results are crap, then they come back beautifull. The original code was because I couldn't get the answer I needed from Bing and Google.
		//Later did the same search and POP, what I wanted was first line. Though my Intellisence is still foobar, and isn't showing all of the overrides. -_-
		DateTimeOffset west = TimeZoneInfo.ConvertTime(adjustedDate,tziw);
		Console.WriteLine("Adjusted to Timezone West:{0}",west);
		DateTime dtNewTest = new DateTime(2015,3,15);
		TimeSpan tsNewTestE = tzie.GetUtcOffset(dtNewTest);
		DateTimeOffset dtoNewTestE = new DateTimeOffset(dtNewTest,tsNewTestE);
		Console.WriteLine("East Constructor: {0}",dtoNewTestE);
		TimeSpan tsNewTestW = tziw.GetUtcOffset(dtNewTest);
		DateTimeOffset dtoNewTestW = new DateTimeOffset(dtNewTest,tsNewTestW);
		Console.WriteLine("West Constructor: {0}",dtoNewTestW);

This entry was posted in Brian's Code Extreme, C# and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s