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

本文详细介绍了如何使用 C# 代码计算日期之间的间隔,包括构造特定日期、计算时间间隔并转换为不同格式的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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类型数值还原为时间类型

 

转载于:https://www.cnblogs.com/zmxmiss/archive/2010/04/24/1719428.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值