C# TimeSpan結構體代碼示例- 純淨天空

文章推薦指數: 80 %
投票人數:10人

本文整理匯總了C#中System.TimeSpan結構體的典型用法代碼示例。

如果您正苦於以下問題:C# TimeSpan結構體的具體用法?C# TimeSpan怎麽用?C# TimeSpan使用的例子? 當前位置:首頁>>代碼示例>>C#>>正文 本文整理匯總了C#中System.TimeSpan結構體的典型用法代碼示例。

如果您正苦於以下問題:C#TimeSpan結構體的具體用法?C#TimeSpan怎麽用?C#TimeSpan使用的例子?那麽恭喜您,這裏精選的結構體代碼示例或許可以為您提供幫助。

TimeSpan結構體屬於System命名空間,在下文中一共展示了TimeSpan結構體的11個代碼示例,這些例子默認根據受歡迎程度排序。

您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於我們的係統推薦出更棒的C#代碼示例。

示例1:DateTime ​點讚15 ​ //Definetwodates. DateTimedate1=newDateTime(2010,1,1,8,0,15); DateTimedate2=newDateTime(2010,8,18,13,30,30); //Calculatetheintervalbetweenthetwodates. TimeSpaninterval=date2-date1; Console.WriteLine("{0}-{1}={2}",date2,date1,interval.ToString()); //DisplayindividualpropertiesoftheresultingTimeSpanobject. Console.WriteLine("{0,-35}{1,20}","ValueofDaysComponent:",interval.Days); Console.WriteLine("{0,-35}{1,20}","TotalNumberofDays:",interval.TotalDays); Console.WriteLine("{0,-35}{1,20}","ValueofHoursComponent:",interval.Hours); Console.WriteLine("{0,-35}{1,20}","TotalNumberofHours:",interval.TotalHours); Console.WriteLine("{0,-35}{1,20}","ValueofMinutesComponent:",interval.Minutes); Console.WriteLine("{0,-35}{1,20}","TotalNumberofMinutes:",interval.TotalMinutes); Console.WriteLine("{0,-35}{1,20:N0}","ValueofSecondsComponent:",interval.Seconds); Console.WriteLine("{0,-35}{1,20:N0}","TotalNumberofSeconds:",interval.TotalSeconds); Console.WriteLine("{0,-35}{1,20:N0}","ValueofMillisecondsComponent:",interval.Milliseconds); Console.WriteLine("{0,-35}{1,20:N0}","TotalNumberofMilliseconds:",interval.TotalMilliseconds); Console.WriteLine("{0,-35}{1,20:N0}","Ticks:",interval.Ticks);開發者ID:.NET開發者,項目名稱:System,代碼行數:20,代碼來源:TimeSpan輸出: 8/18/20101:30:30PM-1/1/20108:00:15AM=229.05:30:15 ValueofDaysComponent:229 TotalNumberofDays:229.229340277778 ValueofHoursComponent:5 TotalNumberofHours:5501.50416666667 ValueofMinutesComponent:30 TotalNumberofMinutes:330090.25 ValueofSecondsComponent:15 TotalNumberofSeconds:19,805,415 ValueofMillisecondsComponent:0 TotalNumberofMilliseconds:19,805,415,000 Ticks:198,054,150,000,000 示例2:TimeSpan ​點讚15 ​ TimeSpaninterval=newTimeSpan(2,14,18); Console.WriteLine(interval.ToString()); //Displays"02:14:18".開發者ID:.NET開發者,項目名稱:System,代碼行數:4,代碼來源:TimeSpan 示例3:DateTime ​點讚15 ​ DateTimedeparture=newDateTime(2010,6,12,18,32,0); DateTimearrival=newDateTime(2010,6,13,22,47,0); TimeSpantravelTime=arrival-departure; Console.WriteLine("{0}-{1}={2}",arrival,departure,travelTime);開發者ID:.NET開發者,項目名稱:System,代碼行數:4,代碼來源:TimeSpan輸出: 6/13/201010:47:00PM-6/12/20106:32:00PM=1.04:15:00 示例4:Random ​點讚13 ​ Randomrnd=newRandom(); TimeSpantimeSpent=TimeSpan.Zero; timeSpent+=GetTimeBeforeLunch(); timeSpent+=GetTimeAfterLunch(); Console.WriteLine("Totaltime:{0}",timeSpent); TimeSpanGetTimeBeforeLunch() { returnnewTimeSpan(rnd.Next(3,6),0,0); } TimeSpanGetTimeAfterLunch() { returnnewTimeSpan(rnd.Next(3,6),0,0); } //Theexampledisplaysoutputlikethefollowing: //Totaltime:08:00:00開發者ID:.NET開發者,項目名稱:System,代碼行數:21,代碼來源:TimeSpan 示例5:foreach ​點讚11 ​ string[]values={"12","31.","5.8:32:16","12:12:15.95",".12"}; foreach(stringvalueinvalues) { try{ TimeSpants=TimeSpan.Parse(value); Console.WriteLine("'{0}'-->{1}",value,ts); } catch(FormatException){ Console.WriteLine("Unabletoparse'{0}'",value); } catch(OverflowException){ Console.WriteLine("'{0}'isoutsidetherangeofaTimeSpan.",value); } }開發者ID:.NET開發者,項目名稱:System,代碼行數:14,代碼來源:TimeSpan輸出: '12'-->12.00:00:00 Unabletoparse'31.' '5.8:32:16'-->5.08:32:16 '12:12:15.95'-->12:12:15.9500000 Unabletoparse'.12' 示例6:ShowFormattingCode ​點讚10 ​ ShowFormattingCode(); //Outputfrom.NETFramework3.5andearlierversions: //12:30:45 //Outputfrom.NETFramework4: //InvalidFormat Console.WriteLine("---"); ShowParsingCode(); //Output: //000000006-->6.00:00:00 voidShowFormattingCode() { TimeSpaninterval=newTimeSpan(12,30,45); stringoutput; try{ output=String.Format("{0:r}",interval); } catch(FormatException){ output="InvalidFormat"; } Console.WriteLine(output); } voidShowParsingCode() { stringvalue="000000006"; try{ TimeSpaninterval=TimeSpan.Parse(value); Console.WriteLine("{0}-->{1}",value,interval); } catch(FormatException){ Console.WriteLine("{0}:BadFormat",value); } catch(OverflowException){ Console.WriteLine("{0}:Overflow",value); } }開發者ID:.NET開發者,項目名稱:System,代碼行數:39,代碼來源:TimeSpan 示例7:Main ​點讚8 ​ //引入命名空間 usingSystem; publicclassExample { publicstaticvoidMain() { AppDomainSetupappSetup=newAppDomainSetup(); appSetup.SetCompatibilitySwitches(newstring[]{"NetFx40_TimeSpanLegacyFormatMode"}); AppDomainlegacyDomain=AppDomain.CreateDomain("legacyDomain", null,appSetup); legacyDomain.ExecuteAssembly("ShowTimeSpan.exe"); } }開發者ID:.NET開發者,項目名稱:System,代碼行數:14,代碼來源:TimeSpan 示例8:Main ​點讚8 ​ //引入命名空間 usingSystem; publicclassExample { publicstaticvoidMain() { TimeSpaninterval=DateTime.Now-DateTime.Now.Date; stringmsg=String.Format("ElapsedTimeToday:{0:d}hours.", interval); Console.WriteLine(msg); } }開發者ID:.NET開發者,項目名稱:System,代碼行數:13,代碼來源:TimeSpan輸出: ElapsedTimeToday:01:40:52.2524662hours. 示例9:newTimeSpan(days,hours,minutes,seconds) ​點讚8 ​ //引入命名空間 usingSystem; classMainClas { publicstaticvoidMain() { inthours=4; intminutes=12; intseconds=10; intdays=1; TimeSpanmyTimeSpan2=newTimeSpan(days,hours,minutes,seconds); Console.WriteLine("myTimeSpan2="+myTimeSpan2); } }開發者ID:C#程序員,項目名稱:System,代碼行數:16,代碼來源:TimeSpan 示例10:newTimeSpan(days,hours,minutes,seconds,milliseconds ​點讚8 ​ //引入命名空間 usingSystem; classMainClas { publicstaticvoidMain() { inthours=4; intminutes=12; intseconds=10; intdays=1; intmilliseconds=20; TimeSpanmyTimeSpan3=newTimeSpan(days,hours,minutes,seconds,milliseconds); Console.WriteLine("myTimeSpan3="+myTimeSpan3); } }開發者ID:C#程序員,項目名稱:System,代碼行數:17,代碼來源:TimeSpan 示例11:newTimeSpan(ticks) ​點讚8 ​ //引入命名空間 usingSystem; classMainClas { publicstaticvoidMain() { longticks=300; TimeSpanmyTimeSpan4=newTimeSpan(ticks); Console.WriteLine("myTimeSpan4="+myTimeSpan4); } }開發者ID:C#程序員,項目名稱:System,代碼行數:14,代碼來源:TimeSpan 注:本文中的System.TimeSpan結構體示例整理自Github/MSDocs等源碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。

相關方法 TimeSpan.TimeSpan() TimeSpan.MaxValue TimeSpan.MinValue TimeSpan.TicksPerDay TimeSpan.TicksPerHour TimeSpan.TicksPerMillisecond TimeSpan.TicksPerMinute TimeSpan.TicksPerSecond TimeSpan.Zero TimeSpan.Days TimeSpan.Hours TimeSpan.Milliseconds TimeSpan.Minutes TimeSpan.Seconds TimeSpan.Ticks TimeSpan.TotalDays TimeSpan.TotalHours TimeSpan.TotalMilliseconds TimeSpan.TotalMinutes TimeSpan.TotalSeconds TimeSpan.Add() TimeSpan.Compare() TimeSpan.CompareTo() TimeSpan.Duration() TimeSpan.Equals() TimeSpan.FromDays() TimeSpan.FromHours() TimeSpan.FromMilliseconds() TimeSpan.FromMinutes() TimeSpan.FromSeconds() TimeSpan.FromTicks() TimeSpan.GetHashCode() TimeSpan.Negate() TimeSpan.Parse() TimeSpan.ParseExact() TimeSpan.Subtract() TimeSpan.ToString() TimeSpan.TryParse() TimeSpan.TryParseExact() TimeSpan.Addition() TimeSpan.Equality() TimeSpan.GreaterThan() TimeSpan.GreaterThanOrEqual() TimeSpan.Inequality() TimeSpan.LessThan() TimeSpan.LessThanOrEqual() TimeSpan.Subtraction() TimeSpan.UnaryNegation() TimeSpan.UnaryPlus()



請為這篇文章評分?