C#时间戳基本用法实例分析
本文实例讲述了C#时间戳基本用法。分享给大家供大家参考。具体如下:
一、C#如何生成一个时间戳
///<summary>
///获取时间戳
///</summary>
///<returns></returns>
publicstaticstringGetTimeStamp()
{
TimeSpants=DateTime.UtcNow-newDateTime(1970,1,1,0,0,0,0);
returnConvert.ToInt64(ts.TotalSeconds).ToString();
}
经常发现很多地方使用一个时间戳表示时间。比如:1370838759 表示2013年6月10日12:32:39。我们就需要一个工具,方便地转换这种时间格式
二、什么是时间戳?
时间戳,又叫UnixStamp.从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒。
三、C#时间戳转换为普通时间
//时间戳转为C#格式时间
privateDateTimeStampToDateTime(stringtimeStamp)
{
DateTimedateTimeStart=TimeZone.CurrentTimeZone.ToLocalTime(newDateTime(1970,1,1));
longlTime=long.Parse(timeStamp+"0000000");
TimeSpantoNow=newTimeSpan(lTime);
returndateTimeStart.Add(toNow);
}
//DateTime时间格式转换为Unix时间戳格式
privateintDateTimeToStamp(System.DateTimetime)
{
System.DateTimestartTime=TimeZone.CurrentTimeZone.ToLocalTime(newSystem.DateTime(1970,1,1));
return(int)(time-startTime).TotalSeconds;
}
希望本文所述对大家的C#程序设计有所帮助。
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短