Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.