c# 当前时间与数字互相转换

1,1900年1月1日

DateTime oldtime = Convert.ToDateTime("1900-1-1 00:00:00");//构造1900年1月1日
TimeSpan ts
= DateTime.Now.Subtract(oldtime);//计算从1900年1月1日到现在的时间间隔
double num = ts.TotalSeconds;//计算出此间隔的总的秒数
Response.Write(num
+"<br/>");
DateTime numtodate = oldtime.AddSeconds(num);//在1900年1月1日基础上添加这个秒数,还原成时间
Response.Write(numtodate.ToString());


2,0000年1月1日

 

DateTime time = new DateTime(2005,12,5);
        Response.Write("ticks:" + time.Ticks+"<br/>");
        Response.Write("time:"+new DateTime(time.Ticks));

DateTime time = new DateTime(2005,12,5);  //构造2005年12月5日的时间类型

Response.Write("ticks:" + time.Ticks+"<br/>"); //time.Ticks为0000年1月1日到2005年12月5日的long类型数值,单位为100豪微秒

 Response.Write("time:"+new DateTime(time.Ticks));//new DateTime(time.Ticks)由long类型数值还原为时间类型

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章