深入U(xiǎn)nix時(shí)間戳與C# DateTime時(shí)間類型互換的詳解
更新時(shí)間:2013年06月05日 12:05:58 作者:
本篇文章是對(duì)Unix時(shí)間戳與C# DateTime時(shí)間類型互換進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
Unix時(shí)間戳最小單位是秒,開(kāi)始時(shí)間為格林威治標(biāo)準(zhǔn)時(shí)間1970-01-01 00:00:00
ConvertIntDateTime方法的基本思路是通過(guò)獲取本地時(shí)區(qū)表示Unixk開(kāi)始時(shí)間,加上Unix時(shí)間值(即過(guò)去的秒數(shù)).
ConvertDateTimeInt方法的基本思路是通過(guò)刻度數(shù)差,再把刻度數(shù)轉(zhuǎn)換為秒數(shù),當(dāng)然要說(shuō)明的是,我這里返回的是double類型,意義上并非是真正的Unix時(shí)間戳格式。
要獲取真正Unix時(shí)間戳的,只獲取整數(shù)部分就可以了。
dangranusing System;
using System.Collections.Generic;
using System.Text;
namespace WWFramework.DateTimes
{
/// <summary>
/// 時(shí)間相關(guān)函數(shù)
/// </summary>
public static class Function
{
/// <summary>
/// 將Unix時(shí)間戳轉(zhuǎn)換為DateTime類型時(shí)間
/// </summary>
/// <param name="d">double 型數(shù)字</param>
/// <returns>DateTime</returns>
public static System.DateTime ConvertIntDateTime(double d)
{
System.DateTime time = System.DateTime.MinValue;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
time = startTime.AddSeconds(d);
return time;
}
/// <summary>
/// 將c# DateTime時(shí)間格式轉(zhuǎn)換為Unix時(shí)間戳格式
/// </summary>
/// <param name="time">時(shí)間</param>
/// <returns>double</returns>
public static double ConvertDateTimeInt(System.DateTime time)
{
double intResult = 0;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
intResult = (time - startTime).TotalSeconds;
return intResult;
}
}
}
ConvertIntDateTime方法的基本思路是通過(guò)獲取本地時(shí)區(qū)表示Unixk開(kāi)始時(shí)間,加上Unix時(shí)間值(即過(guò)去的秒數(shù)).
ConvertDateTimeInt方法的基本思路是通過(guò)刻度數(shù)差,再把刻度數(shù)轉(zhuǎn)換為秒數(shù),當(dāng)然要說(shuō)明的是,我這里返回的是double類型,意義上并非是真正的Unix時(shí)間戳格式。
要獲取真正Unix時(shí)間戳的,只獲取整數(shù)部分就可以了。
復(fù)制代碼 代碼如下:
dangranusing System;
using System.Collections.Generic;
using System.Text;
namespace WWFramework.DateTimes
{
/// <summary>
/// 時(shí)間相關(guān)函數(shù)
/// </summary>
public static class Function
{
/// <summary>
/// 將Unix時(shí)間戳轉(zhuǎn)換為DateTime類型時(shí)間
/// </summary>
/// <param name="d">double 型數(shù)字</param>
/// <returns>DateTime</returns>
public static System.DateTime ConvertIntDateTime(double d)
{
System.DateTime time = System.DateTime.MinValue;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
time = startTime.AddSeconds(d);
return time;
}
/// <summary>
/// 將c# DateTime時(shí)間格式轉(zhuǎn)換為Unix時(shí)間戳格式
/// </summary>
/// <param name="time">時(shí)間</param>
/// <returns>double</returns>
public static double ConvertDateTimeInt(System.DateTime time)
{
double intResult = 0;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
intResult = (time - startTime).TotalSeconds;
return intResult;
}
}
}
相關(guān)文章
基于Unity實(shí)現(xiàn)3D版2048游戲的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Unity實(shí)現(xiàn)簡(jiǎn)易的3D版2048游戲,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,需要的可以參考一下2023-02-02
C#實(shí)現(xiàn)實(shí)體類和XML的相互轉(zhuǎn)換
本文詳細(xì)講解了C#實(shí)現(xiàn)實(shí)體類和XML的相互轉(zhuǎn)換,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02
C#實(shí)現(xiàn)鬧鐘AlarmClock實(shí)例代碼
這篇文章主要介紹了C#實(shí)現(xiàn)鬧鐘AlarmClock實(shí)例代碼,很實(shí)用的功能,需要的朋友可以參考下2014-08-08
winform攔截關(guān)閉按鈕觸發(fā)的事件示例
這篇文章主要介紹了c# winform攔截關(guān)閉按鈕觸發(fā)的事件示例,大家參考使用吧2014-01-01
C#實(shí)現(xiàn)數(shù)字轉(zhuǎn)換漢字的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)數(shù)字轉(zhuǎn)換漢字功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12
帶著問(wèn)題讀CLR via C#(筆記一)CLR的執(zhí)行模型
CLR (Common Language Runtime) 是一個(gè)可以由多種編程語(yǔ)言使用的“運(yùn)行時(shí)”。2013-04-04

