c# 时间戳转化为日期时间
时间: 2023-12-13 09:33:25 浏览: 69
以下是C#中时间戳转化为日期时间的两种方法:
1. 将时间戳转换为秒时间,再转换为日期时间:
```csharp
long timestamp = 1629160713;
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区
DateTime dt = startTime.AddSeconds(timestamp);
Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss")); // 输出:2021-08-17 16:38:33
```
2. 将时间戳转换为毫秒时间,再转换为日期时间:
```csharp
long timestamp = 1629160713;
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区
DateTime dt = startTime.AddMilliseconds(timestamp * 1000);
Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss")); // 输出:2021-08-17 16:38:33
```
相关问题
c#时间戳转换日期格式输出
在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 ]
c# 时间戳转时间方法csdn
在 C# 中,可以使用 `DateTime` 类型的 `AddSeconds` 方法将 Unix 时间戳转换为日期时间。示例代码如下:
```csharp
// Unix 时间戳
long unixTimeStamp = 1612652922;
// 起始时间(1970年1月1日)
DateTime startTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
// 转换为本地时间
DateTime localTime = startTime.AddSeconds(unixTimeStamp).ToLocalTime();
// 输出结果
Console.WriteLine(localTime.ToString("yyyy-MM-dd HH:mm:ss"));
```
注:上述代码中的 `ToLocalTime` 方法用于将 UTC 时间转换为本地时间。如果需要使用其他时区,可以使用 `TimeZoneInfo` 类型的 `ConvertTimeFromUtc` 方法进行转换。
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)