Created
November 7, 2015 19:36
-
-
Save mattjohnsonpint/1b1d986d7765ce2a04b7 to your computer and use it in GitHub Desktop.
Demonstration for StackOverflow question 33579361
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
using NodaTime; | |
using NodaTime.Text; | |
namespace ConsoleApplication10 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Time Zone: " + TimeZoneInfo.Local.DisplayName); | |
Console.WriteLine(); | |
//var input = "2015-11-06T18:34:07+05:30"; | |
var input = "2015-11-07T08:00:00-05:00"; | |
{ | |
DateTime dt = DateTime.Parse(input, | |
CultureInfo.InvariantCulture, | |
DateTimeStyles.AdjustToUniversal); | |
Console.WriteLine("Test 1: {0} {1}", dt, dt.Kind); | |
} | |
{ | |
DateTime dt = DateTime.ParseExact(input, | |
"yyyy-MM-dd'T'HH:mm:ssK", | |
CultureInfo.InvariantCulture, | |
DateTimeStyles.AdjustToUniversal); | |
Console.WriteLine("Test 2: {0} {1}", dt, dt.Kind); | |
} | |
{ | |
DateTimeOffset dto = DateTimeOffset.Parse(input, | |
CultureInfo.InvariantCulture); | |
DateTime dt = dto.UtcDateTime; | |
Console.WriteLine("Test 3: {0} {1}", dt, dt.Kind); | |
} | |
{ | |
DateTimeOffset dto = DateTimeOffset.ParseExact(input, | |
"yyyy-MM-dd'T'HH:mm:sszzz", | |
CultureInfo.InvariantCulture); | |
DateTime dt = dto.UtcDateTime; | |
Console.WriteLine("Test 4: {0} {1}", dt, dt.Kind); | |
} | |
{ | |
OffsetDateTimePattern pattern = OffsetDateTimePattern.ExtendedIsoPattern; | |
OffsetDateTime odt = pattern.Parse(input).Value; | |
DateTimeOffset dto = odt.ToDateTimeOffset(); | |
DateTime dt = dto.UtcDateTime; | |
Console.WriteLine("Test 5: {0} {1}", dt, dt.Kind); | |
} | |
Console.WriteLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: