Created
December 18, 2018 01:51
-
-
Save jingyulong/5d7fc24b5fcfe05b86f70fe7f9f7e5d7 to your computer and use it in GitHub Desktop.
Get TimeStamp in C#
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
// Unix time, which is defined as the number of seconds since midnight (UTC) on 1st January 1970. | |
// The latest version of .Net (v4.6) just added built-in support for Unix time conversions. That includes both to and from Unix time represented by either seconds or milliseconds. | |
// Unix time in seconds to DateTimeOffset: | |
// DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(1000); | |
// DateTimeOffset to Unix time in seconds: | |
// long unixTimeStampInSeconds = dateTimeOffset.ToUnixTimeSeconds(); | |
// Unix time in milliseconds to DateTimeOffset: | |
// DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeMilliseconds(1000000); | |
// DateTimeOffset to Unix time in milliseconds: | |
// long unixTimeStampInMilliseconds = dateTimeOffset.ToUnixTimeMilliseconds(); | |
// Note: These methods convert to and from DateTimeOffset.To get a DateTime representation simply use the DateTimeOffset.DateTime property: | |
// | |
// DateTime dateTime = dateTimeOffset.UtcDateTime; | |
public string GetTimeStamp() | |
{ | |
return DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment