C# Unity 中的时间戳转换
介绍如何在 Unity C# 环境下将时间戳转换为常用的时间格式。
时间戳转换方法
在 Unity C# 中,可以使用 System.DateTime
类提供的功能进行时间戳转换。以下是一些常见转换方法:
-
将时间戳转换为 DateTime 对象:
csharp
long timestamp = 1678441600; // 示例时间戳
System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc).AddSeconds(timestamp);
-
将 DateTime 对象格式化为字符串:
csharp
string formattedDate = dateTime.ToString("yyyy-MM-dd HH:mm:ss"); // 示例格式
注意事项
- 时间戳通常以秒为单位,但也有以毫秒为单位的情况。请根据实际情况进行调整。
System.DateTimeKind.Utc
表示时间戳是基于协调世界时 (UTC)。 如果需要使用本地时间,请使用System.DateTimeKind.Local
。
总结
通过 System.DateTime
类,可以方便地在 Unity C# 中进行时间戳转换。根据实际需求选择合适的转换方法和时间格式。
3.93KB
文件大小:
评论区