C#获取时间戳datetime
时间: 2023-12-01 21:41:04 浏览: 224
获取时间戳
以下是三种C#获取时间戳的方法:
1.方法一:
```csharp
public long GetTimeStamp() {
TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalSeconds);
}
```
2.方法二:
```csharp
public static long GetTimeStamp() {
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalSeconds);
}
```
3.方法三:
```csharp
public static long GetTimeStamp() {
return new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();
}
```
阅读全文