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(, , , ); 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 = "";
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" |
TimeSpan格式化字符串格式(摘)的更多相关文章
- JS 字符串转日期格式 日期格式化字符串
/** * @author 陈维斌 http://www.cnblogs.com/Orange-C/p/4042242.html%20 3 * 如果想将日期字符串格式化,需先将其转换为日期类型Date ...
- 第3.11节 Python强大的字符串格式化新功能:format字符串格式化的格式控制
第3.11节 format字符串格式化的格式控制 一. 引言 上节介绍了四种format进行字符串格式化的 ...
- VBA 格式化字符串 - Format大全
VBA 格式化字符串 VBA 的 Format 函数与工作表函数 TEXT 用法基本相同,但功能更加强大,许多格式只能用于VBA 的 Format 函数,而不能用于工作表函数 TEXT ,以下是本人归 ...
- Python中用format函数格式化字符串
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存. 1.百分号方式 语法:%[( ...
- C#定义类型转化 及 格式化字符串
operator 关键字 operator 关键字用来重载内置运算符,或提供类/结构声明中的用户定义转换.它可以定义不同类型之间采用何种转化方式和转化的结果. operator用于定义类型转化时可采用 ...
- Python格式化字符串~转
Python格式化字符串 在编写程序的过程中,经常需要进行格式化输出,每次用每次查.干脆就在这里整理一下,以便索引. 格式化操作符(%) "%"是Python风格的字符串格式化操作 ...
- 飘逸的python - 增强的格式化字符串format函数
自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱. 语法 它通过{}和 ...
- [转]:C#的ToString如何格式化字符串
C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString("D5") 00025 E 科学型 25000.ToString ...
- C#实现类似"hello $world"的格式化字符串方法
C#自带的string.Format可以格式化字符串,但是还是不太好用,由于格式的字符占位符都是数字,当数目较多时容易混淆.其实可以扩展string的方法,让C#的字符串具备其他的方法,下面介绍一个实 ...
随机推荐
- shell 循环总结
#!/bin/bash my_arry=(a b "c","d" abc) echo "-------FOR循环遍历输出数组--------" ...
- Jmeter 中多线程并发和循环控制器
今天遇到一个场景, 给一个手机号发送短信验证码,通过正确输入短信验证码即登录并获得token,进行其他操作. 短信验证码是4位,即9999个组合, 接口没有对验证次数做校验,所以可以一直一直尝试通过验 ...
- python sort、sorted
1. (1).sorted()方法返回一个新列表(默认升序). list.sort() (2).另一个不同:list.sort()方法仅被定义在list中,sorted()方法对所有的可迭代序列都有效 ...
- Qt5_容器_知识点记录
1.删除: 1.1.erase 1.2.remove / removeAt 2. 3. 4. 5.
- Thunder团队项目视频展示
视频链接:http://v.youku.com/v_show/id_XMzA5MjMzMzcyMA==.html?spm=a2h3j.8428770.3416059.1 视频简介:通过一个小情景开篇, ...
- C#快速生成数据数组
需求:生成一个数组,数组里面的值为1-100实现方式:拿到这个需求很多朋友可能会想到一个快速实现的方式如下: ]; ;i<=;i++){ arr[i]=i; } 但是C#提供了一个快速生成的方式 ...
- Ubuntu 14.04 的 VNC Server
首先,如果是Desktop 版本的 Ubuntu,不需要另外安装vnc server. 网上也不知怎么搞的,一堆奇怪的方法,要安装TightVNCServer,然后一堆sb设置 然后,主要有两个配置 ...
- 微服务设计 - api版本控制
要描述了几种API版本控制的方法.用户可以查询原始的API,或者添加定制的头文件来接收特定的版本.如果应用程序收到一个重大修订,将URI修改为V2.在进行迭代改进时,将创建与更改日期相一致的端点,并允 ...
- 雷林鹏分享:C# 类(Class)
C# 类(Class) 当您定义一个类时,您定义了一个数据类型的蓝图.这实际上并没有定义任何的数据,但它定义了类的名称意味着什么,也就是说,类的对象由什么组成及在这个对象上可执行什么操作.对象是类的实 ...
- Python基础--Python简介和入门
☞写在前面 在说Python之前,我想先说一下自己为什么要学Python,我本人之前也了解过Python,但没有深入学习.之前接触的语言都是Java,也写过一些Java自动化用例,对Java语言只能说 ...