I have had look around stackoverflow, and even looked at some of the suggested questions and none seem to answer, how do you get a unix timestamp in C#?
C# – How to get the unix timestamp in C#
ctimestampunix-timestamp
Related Topic
- C# – How to enumerate an enum
- Javascript – How to get a timestamp in JavaScript
- Mysql – Should I use the datetime or timestamp data type in MySQL
- C# – Case insensitive ‘Contains(string)’
- C# – ‘Static readonly’ vs. ‘const’
- Python – Converting unix timestamp string to readable date
- C# – Error – Unable to access the IIS metabase
- C# – \d less efficient than [0-9]
Best Answer
As of .NET 4.6, there is
DateTimeOffset.ToUnixTimeSeconds
.This is an instance method, so you are expected to call it on an instance of
DateTimeOffset
. You can also cast any instance ofDateTime
, though beware the timezone. To get the current timestamp:To get the timestamp from a
DateTime
: