深入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)文章
C#讀取xml節(jié)點(diǎn)數(shù)據(jù)方法小結(jié)
這篇文章主要介紹了C#讀取xml節(jié)點(diǎn)數(shù)據(jù)的方法,實(shí)例總結(jié)了C#針對(duì)XML文件節(jié)點(diǎn)操作的相關(guān)技巧,需要的朋友可以參考下2015-06-06C#判斷某個(gè)軟件是否已安裝實(shí)現(xiàn)代碼分享
這篇文章主要介紹了C#判斷某個(gè)軟件是否已安裝實(shí)現(xiàn)代碼分享,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-06-06c#生成excel示例sql數(shù)據(jù)庫(kù)導(dǎo)出excel
這篇文章主要介紹了c#操作excel的示例,里面的方法可以直接導(dǎo)出數(shù)據(jù)到excel,大家參考使用吧2014-01-01Unity實(shí)現(xiàn)物體沿自身的任意軸向旋轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)物體沿自身的任意軸向旋轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01C#使用迭代器實(shí)現(xiàn)文字動(dòng)態(tài)效果的示例代碼
這篇文章主要為大家詳細(xì)介紹了C#如何通過(guò)使用迭代器實(shí)現(xiàn)文字動(dòng)態(tài)效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02C# 并行和多線程編程——Task進(jìn)階知識(shí)
這篇文章主要介紹了C# 并行和多線程編程——Task進(jìn)階知識(shí)的的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-02-02