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);
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.jsjavascript
深度閱讀:html
C# TimeSpan:https://msdn.microsoft.com/zh-cn/library/system.timespan.aspxjava
js求時間差:http://www.cnblogs.com/cm490893449/archive/2010/10/26/1861230.htmlthis