C#获取时间戳datetime
时间: 2023-12-01 10:41:04 浏览: 223
C# DateTime与时间戳转换实例
以下是三种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();
}
```
阅读全文