Skip to content

Instantly share code, notes, and snippets.

@betweenbrain
Created December 22, 2015 15:14
Show Gist options
  • Save betweenbrain/8885af32e9529fef02f3 to your computer and use it in GitHub Desktop.
Save betweenbrain/8885af32e9529fef02f3 to your computer and use it in GitHub Desktop.
C# Offset UTC by Int Console App
//Current UTC time
DateTime timeUtc = DateTime.UtcNow;
Console.WriteLine("UTC is {0}", timeUtc);
// UTC offset as a +/- integer
Console.WriteLine("Enter an +/- integr to offset UTC:");
int offset = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Int offset is {0}", offset);
// Format UTC offset as hh:mm:ss
string offsetTime = offset.ToString("00") + ":00:00";
Console.WriteLine("Int to dateTime {0}", offsetTime);
// Parse hh:mm:ss offset string as time
TimeSpan utcOffset = TimeSpan.Parse(offsetTime);
// Calculate time as offset UTC
DateTime newDateTime = timeUtc + utcOffset;
Console.WriteLine("UTC offset by {0} is {1}", utcOffset, newDateTime);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment