以为我可以分享:public static string ToReadableAgeString(this TimeSpan span){ return string.Format("{0:0}", span.Days / 365.25);}public static string ToReadableString(this TimeSpan span){ string formatted = string.Format("{0}{1}{2}{3}", &nb...
outputBlock.Text+= String.Format("{0} --> {1}", value, interval.ToString("c")) +Environment.NewLine;elseoutputBlock.Text+= String.Format("Unable to parse '{0}'", value) +Environment.NewLine; value="16:32.05";if(TimeSpan.TryParseExact(value,@"mm\:ss\.ff",null,outinterval)) outp...
usingSystem;publicclassExample{publicstaticvoidMain(){stringvalue="1.03:14:56.1667"; TimeSpan interval;try{ interval = TimeSpan.ParseExact(value,"c",null); Console.WriteLine("Converted '{0}' to {1}",value, interval); } catch (FormatException) { Console.WriteLine("{0}: Bad Format",value)...
usingSystem;publicclassExample{publicstaticvoidMain(){stringvalue="1.03:14:56.1667"; TimeSpan interval;try{ interval = TimeSpan.ParseExact(value,"c",null); Console.WriteLine("Converted '{0}' to {1}",value, interval); } catch (FormatException) { Console.WriteLine("{0}: Bad Format",value)...
String 目前TimeSpan值的字串表示。 範例 下列範例會顯示使用數個TimeSpan值呼叫ToString方法所傳回的字串。 請注意,雖然此範例不會直接呼叫ToString方法,但是當Console.WriteLine方法嘗試將TimeSpan值轉換成其字串表示法時,就會呼叫此方法。 C# TimeSpan span;// Initialize a time span to zero.span = TimeSpan....
TimeSpan span; // Initialize a time span to zero. span = TimeSpan.Zero; Console.WriteLine(span); // Initialize a time span to 14 days. span = new TimeSpan(-14, 0, 0, 0, 0); Console.WriteLine(span); // Initialize a time span to 1:02:03. span = new TimeSpan(1, 2, 3);...
(instantStart,instantEnd);// 计算时间差longhours=duration.toHours();// 获取时间差的小时部分longminutes=duration.toMinutes()%60;// 获取时间差的分钟部分longseconds=duration.getSeconds()%60;// 获取时间差的秒部分Stringtimespan=String.format("%d小时 %d分钟 %d秒",hours,minutes,seconds);// 格式化...
public string DateStringFromNow(DateTime dt) { TimeSpan span = DateTime.Now - dt; if (span.TotalDays > 60) { return dt.ToShortDateString(); } else if ( span.TotalDays > 30 ) { return "1个月前"; } else if (span.TotalDays > 14) ...
String 当前TimeSpan值的字符串表示形式。 示例 以下示例显示通过调用ToString具有多个值的 方法返回的TimeSpan字符串。 请注意,尽管该示例不直接调用ToString方法,但它在尝试将值转换为TimeSpan其字符串表示形式时由Console.WriteLine方法调用。 C# TimeSpan span;// Initialize a time span to zero.span = TimeSpan...
Left) => "---to truncate" ###Format String You can format a string using the FormatWith() method: "To be formatted -> {0}/{1}.".FormatWith(1, "A") => "To be formated -> 1/A." This is an extension method based on String.Format, so exact rules applies to it. If format...