Skip to content

Instantly share code, notes, and snippets.

@jingyulong
Created December 18, 2018 01:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jingyulong/5d7fc24b5fcfe05b86f70fe7f9f7e5d7 to your computer and use it in GitHub Desktop.
Save jingyulong/5d7fc24b5fcfe05b86f70fe7f9f7e5d7 to your computer and use it in GitHub Desktop.
Get TimeStamp in C#
// 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