在 Rust 中,你可以使用 `chrono` 库来进行时间戳转换。这里提供完整的解决方案:## Cargo.toml 依赖:
```toml
[dependencies]
chrono = { version = "0.4", features = ["serde"] }
chrono-tz = "0.8"
```
## 使用说明:
1. **时间戳格式**:`1753790400000` 是13位毫秒级时间戳
2. **转换步骤**:
- 字符串转数字
- 毫秒转秒(除以1000)
- 创建UTC时间
- 转换为北京时间(+8时区)
- 格式化输出
## 运行结果:
```
时间戳: 1753790400000
北京时间: 2025-07-29 20:00:00
```
## 其他常用格式:
```rust
// 包含星期几
beijing_datetime.format("%Y-%m-%d %H:%M:%S %A").to_string()
// 输出: 2025-07-29 20:00:00 Tuesday
// ISO 8601 格式
beijing_datetime.format("%Y-%m-%dT%H:%M:%S%z").to_string()
// 输出: 2025-07-29T20:00:00+0800
```
这个解决方案可以准确地将毫秒级时间戳转换为北京时间,并且包含了错误处理。