TimeSpan格式化字符串格式(摘)

简介: 原文:TimeSpan格式化字符串格式(摘)一直在用DateTime, 却不常用TimeSpan , 今天突然用到了, 发现不知道咋做格式化...百度一下,找到了答案, 在这记录一下, 免得以后找花费时间 以下内容摘抄自 Microsoft Docs  原文地址: https://docs.
原文: TimeSpan格式化字符串格式(摘)

一直在用DateTime, 却不常用TimeSpan , 今天突然用到了, 发现不知道咋做格式化...百度一下,找到了答案, 在这记录一下, 免得以后找花费时间

以下内容摘抄自 Microsoft Docs  原文地址: https://docs.microsoft.com/en-us/previous-versions/windows/silverlight/dotnet-windows-silverlight/ee372287(v=vs.95)

分别展示了ToString方法跟string.Format方法中的方法, 其中string.Format的用法可以在mvc的Html.TextBox的format参数中使用

这里只记录下基本用法, 更多使用参考请移步上方链接.

TimeSpan转字符串

using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      TimeSpan duration = new TimeSpan(1, 12, 23, 62);

      string output = null;
      output = "Time of Travel: " + duration.ToString("%d") + " days";
      outputBlock.Text += output + Environment.NewLine;
      output = "Time of Travel: " + duration.ToString(@"dd\.hh\:mm\:ss"); 
      outputBlock.Text += output + Environment.NewLine;

      outputBlock.Text += String.Format("Time of Travel: {0:%d} day(s)", 
                                        duration) + Environment.NewLine;
      outputBlock.Text += String.Format("Time of Travel: {0:dd\\.hh\\:mm\\:ss} days", 
                                        duration) + Environment.NewLine;
   }
}
// The example displays the following output:
//       Time of Travel: 1 days
//       Time of Travel: 01.12:24:02
//       Time of Travel: 1 day(s)
//       Time of Travel: 01.12:24:02 days

 

字符串转TimeSpan

using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string value = null;
      TimeSpan interval;

      value = "6";
      if (TimeSpan.TryParseExact(value, "%d", null, out interval))
         outputBlock.Text += String.Format("{0} --> {1}", value, 
                                           interval.ToString("c")) + Environment.NewLine;
      else
         outputBlock.Text += String.Format("Unable to parse '{0}'", value) + Environment.NewLine;

      value = "16:32.05";
      if (TimeSpan.TryParseExact(value, @"mm\:ss\.ff", null, out interval))
         outputBlock.Text += String.Format("{0} --> {1}", value, 
                                           interval.ToString("c")) + Environment.NewLine;
      else
         outputBlock.Text += String.Format("Unable to parse '{0}'", value) + Environment.NewLine;

      value= "12.035";
      if (TimeSpan.TryParseExact(value, "ss\\.fff", null, out interval))
         outputBlock.Text += String.Format("{0} --> {1}", value, 
                                           interval.ToString("c")) + Environment.NewLine;
      else
         outputBlock.Text += String.Format("Unable to parse '{0}'", value) + Environment.NewLine;
   }
}
// The example displays the following output:
//       6 --> 6.00:00:00
//       16:32.05 --> 00:16:32.0500000
//       12.035 --> 00:00:12.0350000

 

参数表格

The following table describes the custom date and time format specifiers.

 

Format specifier

Description

Example

"d", "%d"

The number of whole days in the time interval.

More information: The "d" Custom Format Specifier.

new TimeSpan(6, 14, 32, 17, 685):

   %d --> "6"

   d\.hh\:mm --> "6.14:32"

"dd"-"dddddddd"

The number of whole days in the time interval, padded with leading zeros as needed.

More information: The "dd"-"dddddddd" Custom Format Specifiers.

new TimeSpan(6, 14, 32, 17, 685):

   ddd --> "006"

   dd\.hh\:mm --> "06.14:32"

"h", "%h"

The number of whole hours in the time interval that are not counted as part of days. Single-digit hours do not have a leading zero.

More information: The "h" Custom Format Specifier.

new TimeSpan(6, 14, 32, 17, 685):

   %h --> "14"

   hh\:mm --> "14:32"

"hh"

The number of whole hours in the time interval that are not counted as part of days. Single-digit hours have a leading zero.

More information: The "hh" Custom Format Specifier.

new TimeSpan(6, 14, 32, 17, 685):

   hh --> "14"

new TimeSpan(6, 8, 32, 17, 685):

   hh --> 08

"m", "%m"

The number of whole minutes in the time interval that are not included as part of hours or days. Single-digit minutes do not have a leading zero.

More information: The "m" Custom Format Specifier.

new TimeSpan(6, 14, 8, 17, 685):

   %m --> "8"

   h\:m --> "14:8"

"mm"

The number of whole minutes in the time interval that are not included as part of hours or days. Single-digit minutes have a leading zero.

More information: The "mm" Custom Format Specifier.

new TimeSpan(6, 14, 8, 17, 685):

   mm --> "08"

new TimeSpan(6, 8, 5, 17, 685):

   d\.hh\:mm\:ss --> 6.08:05:17

"s", "%s"

The number of whole seconds in the time interval that are not included as part of hours, days, or minutes. Single-digit seconds do not have a leading zero.

More information: The "s" Custom Format Specifier.

TimeSpan.FromSeconds(12.965):

   %s --> 12

   s\.fff --> 12.965

"ss"

The number of whole seconds in the time interval that are not included as part of hours, days, or minutes. Single-digit seconds have a leading zero.

More information: The "ss" Custom Format Specifier.

TimeSpan.FromSeconds(6.965):

   ss --> 06

   ss\.fff --> 06.965

"f", "%f"

The tenths of a second in a time interval.

More information: The "f" Custom Format Specifier.

TimeSpan.FromSeconds(6.895):

   f --> 8

   ss\.f --> 06.8

"ff"

The hundredths of a second in a time interval.

More information: The "ff" Custom Format Specifier.

TimeSpan.FromSeconds(6.895):

   ff --> 89

   ss\.ff --> 06.89

"fff"

The milliseconds in a time interval.

More information: The "fff" Custom Format Specifier.

TimeSpan.FromSeconds(6.895):

   fff --> 895

   ss\.fff --> 06.895

"ffff"

The ten-thousandths of a second in a time interval.

More information: The "ffff" Custom Format Specifier.

TimeSpan.Parse("0:0:6.8954321"):

   ffff --> 8954

   ss\.ffff --> 06.8954

"fffff"

The hundred-thousandths of a second in a time interval.

More information: The "fffff" Custom Format Specifier.

TimeSpan.Parse("0:0:6.8954321"):

   fffff --> 89543

   ss\.fffff --> 06.89543

"ffffff"

The millionths of a second in a time interval.

More information: The "ffffff" Custom Format Specifier.

TimeSpan.Parse("0:0:6.8954321"):

   ffffff --> 895432

   ss\.ffffff --> 06.895432

"fffffff"

The ten-millionths of a second (or the fractional ticks) in a time interval.

More information: The "fffffff" Custom Format Specifier.

TimeSpan.Parse("0:0:6.8954321"):

   fffffff --> 8954321

   ss\.fffffff --> 06.8954321

"F", "%F"

The tenths of a second in a time interval. Nothing is displayed if the digit is zero.

More information: The "F" Custom Format Specifier.

TimeSpan.Parse("00:00:06.32"):

   %F: 3

TimeSpan.Parse("0:0:3.091"):

   ss\.F: 03.

"FF"

The hundredths of a second in a time interval. Any fractional trailing zeros or two zero digits are not included.

More information: The "FF" Custom Format Specifier.

TimeSpan.Parse("00:00:06.329"):

   FF: 32

TimeSpan.Parse("0:0:3.101"):

   ss\.FF: 03.1

"FFF"

The milliseconds in a time interval. Any fractional trailing zeros are not included.

More information:

TimeSpan.Parse("00:00:06.3291"):

   FFF: 329

TimeSpan.Parse("0:0:3.1009"):

   ss\.FFF: 03.1

"FFFF"

The ten-thousandths of a second in a time interval. Any fractional trailing zeros are not included.

More information: The "FFFF" Custom Format Specifier.

TimeSpan.Parse("00:00:06.32917"):

   FFFFF: 3291

TimeSpan.Parse("0:0:3.10009"):

   ss\.FFFF: 03.1

"FFFFF"

The hundred-thousandths of a second in a time interval. Any fractional trailing zeros are not included.

More information: The "FFFFF" Custom Format Specifier.

TimeSpan.Parse("00:00:06.329179"):

   FFFFF: 32917

TimeSpan.Parse("0:0:3.100009"):

   ss\.FFFFF: 03.1

"FFFFFF"

The millionths of a second in a time interval. Any fractional trailing zeros are not displayed.

More information: The "FFFFFF" Custom Format Specifier.

TimeSpan.Parse("00:00:06.3291791"):

   FFFFFF: 329179

TimeSpan.Parse("0:0:3.1000009"):

   ss\.FFFFFF: 03.1

"FFFFFFF"

The ten-millions of a second in a time interval. Any fractional trailing zeros or seven zeros are not displayed.

More information: The "FFFFFFF" Custom Format Specifier.

TimeSpan.Parse("00:00:06.3291791"):

   FFFFFF: 3291791

TimeSpan.Parse("0:0:3.1900000"):

   ss\.FFFFFF: 03.19

'string'

Literal string delimiter.

More information: Other Characters.

new TimeSpan(14, 32, 17):

   hh':'mm':'ss --> "14:32:17"

\

The escape character.

More information: Other Characters.

new TimeSpan(14, 32, 17):

   hh\:mm\:ss --> "14:32:17"

Any other character

Any other unescaped character is interpreted as a custom format specifier.

More Information: Other Characters.

new TimeSpan(14, 32, 17):

   hh\:mm\:ss --> "14:32:17"

 

目录
相关文章
|
7月前
|
存储 Unix 编译器
%[ ]格式说明符巧用
%[ ]格式说明符巧用
38 0
QString格式化的几种用法:补零/进制转换/保留小数位等
QString格式化的几种用法:补零/进制转换/保留小数位等
2660 0
|
2月前
让星星⭐月亮告诉你,打印完全格式的日期格式 包含 年月日时分秒毫秒
此方法用于获取完整的日期时间格式,返回值形如“2020-06-30 15:15:15:015”,精确到毫秒。通过`Calendar`类获取当前时间的年、月、日、时、分、秒及毫秒,并将其格式化拼接成字符串返回。
30 1
|
7月前
|
机器学习/深度学习 Python
关于python字符串format的一些花式用法_format带加号
关于python字符串format的一些花式用法_format带加号
|
6月前
|
C++
【洛谷 B2025】输出字符菱形 题解(raw string literal)
使用`*`构建一个斜置的、对角线长度为5的菱形。无输入要求。输出示例:`*`、`***`、`*****`、`***`、` *`。代码实现使用C++,直接打印预定义字符串完成。
84 0
优雅的99乘法表以及format使用
优雅的99乘法表以及format使用
|
数据格式
labview数据类型转换字符串文件路径进制转换小数格式等
labview数据类型转换字符串文件路径进制转换小数格式等
284 0
|
存储 SQL JSON
不全?MySQL数据类型精讲,定点日期枚举文本字符串,json二进制,空间,选择建议,完整详细可收藏
不全?MySQL数据类型精讲,定点日期枚举文本字符串,json二进制,空间,选择建议,完整详细可收藏
331 1
不全?MySQL数据类型精讲,定点日期枚举文本字符串,json二进制,空间,选择建议,完整详细可收藏
|
C语言
C语言:输入年月日,输出这是这一年的第几天
C语言:输入年月日,输出这是这一年的第几天
180 0
|
Python
python编程:format格式输出笔记-1_
python编程:format格式输出笔记-1_
123 0
python编程:format格式输出笔记-1_

热门文章

最新文章