c#时间戳转换日期格式输出
时间: 2023-10-28 07:24:22 浏览: 155
在C#中,可以使用以下代码将时间戳转换为日期格式输出:
```csharp
using System;
using UnityEngine;
private void Start()
{
DateTime dateTime = TimestampHelper.GetDateTime(1482115779);
Debug.Log(dateTime.Year + "年" + dateTime.Month + "月" + dateTime.Day + "日");
DateTime dateTime_2 = TimestampHelper.GetDateTime("1482115779");
Debug.Log(dateTime_2.Hour + ":" + dateTime.Minute + ":" + dateTime.Second);
}
public static DateTime GetTime(long TimeStamp, bool AccurateToMilliseconds = false)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
if (AccurateToMilliseconds)
{
return startTime.AddTicks(TimeStamp * 10000);
}
else
{
return startTime.AddTicks(TimeStamp * 10000000);
}
}
```
这段代码使用了一个名为`TimestampHelper`的静态类来实现时间戳转换日期的功能。在`Start()`方法中,我们首先使用`TimestampHelper.GetDateTime()`方法将时间戳转换为`DateTime`类型的日期时间对象,然后通过访问`DateTime`对象的属性来获取具体的年、月、日等信息,并通过`Debug.Log()`方法将其输出。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [C# 工具类:时间戳转换日期格式](https://blog.csdn.net/qq_43505432/article/details/114676442)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [C# 时间戳转日期](https://blog.csdn.net/weixin_44171249/article/details/125658767)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [C# DateTime与时间戳转换实例](https://download.csdn.net/download/weixin_38638312/12778561)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文