日期:2014-05-18  浏览次数:20416 次

求一个将时间差转换为几天几小时几分的函数!!
求一个将时间差转换为几天几小时几分的函数!!
如当前时间-减某个时间=4.14:25:04.9981250


------解决方案--------------------
晓习 帮顶
------解决方案--------------------
timespan.days timespan.hours
------解决方案--------------------
时间相减, 得到一个TimeSpan的数据类型, 这个数据类型里已经包含楼主想要的信息
如: TotalDays, TotalHours
------解决方案--------------------
剩 余 时 间 :
<input name="Input" id="time" style="border:none; color:#FF0000" value="-15314325" readonly>
JScript code
<script language=JavaScript>
              var tt="<%# etime(DataBinder.Eval(Container.DataItem,"etime").ToString().Trim())%>";
              DateTime = new Array(); 
 DateTime = tt.split("-");
 var year = DateTime[0];
 var month = DateTime[1];
 var day = DateTime[2];
 var hour = DateTime[3];
 var minitue = DateTime[4];
 var second = DateTime[5];
 
 
 var Time = new Date(year,month - 1,day,hour,minitue,second);

 
var auctionDate = -15314325;
var startTime = (new Date("2009/1/2")).getTime();
var Temp;
var timerID = null;
var timerRunning = false;
function showtime(){
now = new Date();
var ts=parseInt((Time.getTime()-now.getTime())/1000)+auctionDate;
var dateLeft = 0;
var hourLeft = 0;
var minuteLeft = 0;
var secondLeft = 0;
if(ts < 0){
ts = 0;
CurHour = 0;
CurMinute = 0;
CurSecond = 0;}
else {
dateLeft =parseInt(ts/86400);
ts = ts - dateLeft * 86400;
hourLeft = parseInt(ts/3600);
ts = ts - hourLeft * 3600;
minuteLeft = parseInt(ts/60);
secondLeft = ts - minuteLeft * 60;}
if(hourLeft < 10) hourLeft = '0' +hourLeft;
if(minuteLeft < 10) minuteLeft = '0' +minuteLeft;
if(secondLeft<10) secondLeft='0'+secondLeft;
if( dateLeft > 0 )
dateLeft = dateLeft + "天" ;
else
dateLeft = "";
if( hourLeft > 0 )
hourLeft = hourLeft + "小时" ;
else{
if( dateLeft != "" )
hourLeft = "00小时";
else
hourLeft = "";}
if( minuteLeft > 0 )
minuteLeft = minuteLeft + "分钟" ;
else{
if( dateLeft !="" || hourLeft != "")
minuteLeft = "00分钟";
else
minuteLeft = "";}
if( secondLeft > 0 )
secondLeft = secondLeft + "秒" ;
else{
if( dateLeft !="" || hourLeft != "" || minuteLeft != "")
secondLeft = "00秒";
else
secondLeft = "";}
//if (dateLeft == '') {
Temp=dateLeft+hourLeft+minuteLeft+secondLeft ;
//}else {
//Temp=dateLeft+hourLeft;}
if(dateLeft <=0 && hourLeft<=0 && minuteLeft<=0 && secondLeft <=0){
Temp = "结束";
stopclock();}
if (document.getElementById('time')) document.getElementById('time').value=Temp;
timerID = setTimeout("showtime()",1000);
timerRunning = true;}
var timerID = null;
var timerRunning = false;
function stopclock(){
if(timerRunning)
clearTimeout(timerID);
timerRunning = false;}
function macauclock(){
stopclock();
showtime();}
function onloadall(){
macauclock();
try{
initprovcity(); }
catch(e){
}}
try{
onload=onloadall();}
catch(e){}
  </script>

------解决方案--------------------
C# code

            DateTime dt1 = DateTime.Now;
            DateTime dt2 = new DateTime(2008, 8, 8, 20, 0, 0);
            TimeSpan ts = dt1 - dt2;
            Console.Write("相差{0}日 {1}时{2}分{3}秒", ts.Days, ts.Hours, ts.Minutes, ts.Seconds);

------解决方案--------------------
DateTime dt1 = DateTime.Now;
DateTime dt2 = new DateTime(2008, 8, 8, 20, 0, 0);
TimeSpan ts = dt1 - dt2;
Console.Write("相差{0}日 {1}时{2}分{3}秒", ts.Days, ts.Hours, ts.Minutes, ts.Seconds);