时间差(C#、js)
- C#中的时间差:TimeSpan
1 string strBirthday = "1987-10-01 00:00:00"; 2 string strThisYearBirthday = "2015-10-01 00:00:00"; 3 4 DateTime now = DateTime.Now; 5 DateTime thisYearBirthday = DateTime.Parse(strThisYearBirthday); 6 TimeSpan ts = thisYearBirthday - now;//时间差 7 8 string result = string.Format("距离{0}生日还有: ", thisYearBirthday.Year); 9 result += string.Format("{0}天{1}小时{2}分{3}秒<br /><br />", 10 ts.Days, 11 ts.Hours, 12 ts.Minutes, 13 ts.Seconds); 14 Response.Write(result); 15 16 TimeSpan tsAge = now - DateTime.Parse(strBirthday); 17 string result2 = string.Format("你在这个世界上已经:"); 18 result2 += string.Format("{0}年{1}月{2}天{3}小时{4}分{5}秒<br /><br />", 19 Math.Floor(tsAge.TotalDays / 365), 20 Math.Floor((tsAge.TotalDays % 365) / 31), 21 Math.Floor((tsAge.TotalDays % 365) % 31), 22 tsAge.Hours, 23 tsAge.Minutes, 24 tsAge.Seconds); 25 Response.Write(result2);
- Javascript 中的时间差
1 <script type="text/javascript"> 2 var now = new Date();//当前时间 3 var target = new Date(2015, 9, 1, 0, 0, 0);//js的月份从0开始,到11结束 4 5 //调用方式1:传入时间戳Number类型 6 var timespan = new TimeSpan(now.getTime(), target.getTime()); 7 //调用方式2:传入时间Date类型 8 var timespan = new TimeSpan(now, target); 9 10 var result = "距离" + target.toLocaleDateString() + target.toTimeString() + "还有:<br />" 11 + timespan.days() + "天" 12 + timespan.hours() + "小时" 13 + timespan.minutes() + "分" 14 + timespan.seconds() + "秒"; 15 document.write(result); 16 17 </script>
文件下载:TimeSpan.js
深度阅读:
C# TimeSpan:https://msdn.microsoft.com/zh-cn/library/system.timespan.aspx
js求时间差:http://www.cnblogs.com/cm490893449/archive/2010/10/26/1861230.html