Created
January 5, 2017 12:58
-
-
Save pietrom/d0af2d5e6f2493fddab265b8a24a5e05 to your computer and use it in GitHub Desktop.
This file contains hidden or 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; | |
namespace DateAndTimeMeltingPot { | |
class MainClass { | |
public static void Main(string[] args) { | |
// Getting 'calendar/clock' date adn time | |
DateTime myBirthday = new DateTime(1978, 3, 19, 1, 15, 0, DateTimeKind.Unspecified); | |
Print(myBirthday); | |
// Getting 'now' as 'millis since epoch' | |
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); | |
long now = Convert.ToInt64((DateTime.UtcNow - epoch).TotalMilliseconds); | |
// Getting 'now' as DateTime instance on system time zone | |
DateTime nowAsDateAndTime = DateTime.Now; | |
Print(nowAsDateAndTime); | |
// Converting DateTime from one time zone to another one | |
DateTime nowAsUtcDateTime = TimeZoneInfo.ConvertTimeToUtc(nowAsDateAndTime); | |
Print(nowAsUtcDateTime); | |
DateTime nowInNewZealand = TimeZoneInfo.ConvertTimeFromUtc(nowAsUtcDateTime, TimeZoneInfo.FindSystemTimeZoneById("New Zealand Standard Time")); | |
Print(nowInNewZealand); | |
// Parsing from ISO | |
DateTime parsedLocal = DateTime.Parse("1978-03-19T01:15:00.000Z", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); | |
Print(parsedLocal); | |
DateTime parsedUtc= DateTime.Parse("1978-03-19T01:15:00.000Z", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal); | |
Print(parsedUtc); | |
Console.ReadKey(); | |
} | |
private static void Print(DateTime dt) { | |
Console.WriteLine($"{dt} {dt.Kind}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment