How to get Unix timestamp in C#
Hello everyone! I'm a beginner C# developer, and I've encountered a problem working with dates and times. I need to get a Unix timestamp — the number of seconds that have passed since January 1, 1970 (Unix epoch). This is needed for integration with an external API that accepts time in this format.
I tried something like:
```csharp
DateTime now = DateTime.Now;
long timestamp = (long)now; // Error: cannot implicitly convert
```
But the compiler gives an error that DateTime cannot be converted to long. Then I tried using the Ticks property:
```csharp
long timestamp = DateTime.Now.Ticks;
```
But the value turns out to be huge (e.g., 638700000000000000), and this is clearly not a Unix timestamp. I understand that Ticks counts from January 1, 0001, not from 1970.
Please tell me how to get
No answers yet.
Log in to leave an answer