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

TimeSpan格式化字符串格式(摘)
原文: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;elseoutputBlock.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;elseoutputBlock.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;elseoutputBlock.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"

 

posted on 2018-04-16 12:02 NET未来之路 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/8855221.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/570943.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

关于ElasticSearch处理过滤条件

在用es做搜索时通常会遇到很多条件过滤查询,以及一些复杂的请求参数过滤。 简单以一组数据类型举例。请求参数为: {关键字:关键字,品牌:品牌,分类:分类,规格:{规格1:规格1&#xf…

Layer笔记

官网地址&#xff1a;http://layer.layui.com/hello.html 引入代码 <script src"jQuery的路径"></script> <!-- 你必须先引入jQuery1.8或以上版本 --><script src"layer.js的路径"></script> 1.loading // 开始加载 var ind…

关于feign开启hystrix导致用户鉴权失败

关于feign开启hystrix熔断导致用户鉴权失败的原因是&#xff1a; feign的hystrix熔断默认机制是线程池隔离。而代码在获取用户权限信息时又是线程池处理&#xff0c;所以导致每次获取用户信息为null. 处理办法是&#xff1a; 变更hystrix的隔离策略为信号量策略。 SEMAPHOR…

复习……方法的重载

Lesson Six                         2018-04-20  00:48:57 1.一个项目或工程&#xff0c;一定是由一个一个类构成的。2.类是抽象的&#xff0c;比如建筑图纸。而具体的建筑&#xff0c;是根据图纸建设成的&#xff0c;实际上就是类的实例化3.完成一…

对Kafka的总结

定位&#xff1a;kafka是一款分布式&#xff0c;高吞吐量&#xff0c;基于发布/订阅的消息中间件。 核心组件&#xff1a; broker&#xff1a;kafka服务器&#xff0c;负责消息的存储和转发。 topic&#xff1a;主题&#xff0c;消息的类别&#xff0c;kafka按照topic分类消…

[转]Nginx的负载均衡方式

如果Nginx没有仅仅只能代理一台服务器的话&#xff0c;那它也不可能像今天这么火&#xff0c;Nginx可以配置代理多台服务器&#xff0c;当一台服务器宕机之后&#xff0c;仍能保持系统可用。具体配置过程如下&#xff1a; 1. 在http节点下&#xff0c;添加upstream节点。 upstr…

Ribbon中的负载均衡算法实现

Ribbon响应时间权重负载均衡算法&#xff0c;假设有3台服务器A,B&#xff0c;C响应时间为10&#xff0c;40&#xff0c;80ms。 算法公式&#xff1a;weighsofar 总响应时长- 本服务器平均响应时长 A&#xff1a;0130-10120 B:120130-40210 C:210130-80260 将3个轮询数值放…

maven私服配置

环境&#xff1a; eclipse 、maven、nexus。 1、配置setting.xml文件 1.1、配置本地仓库位置&#xff1a;文件中&#xff0c;存在节点 “localRepository”&#xff0c;默认是注释&#xff0c;也就是本地仓库使用默认地址“Default: ~/.m2/repository”&#xff0c;一般为系统…

算法练习一:求最小公倍数

求最小公倍数&#xff0c; 两个数的乘积2个数的最大公约数*最小公倍数 最大公约数的求法&#xff1a;辗转相除法。 2个数a和b&#xff0c;最大公约数等于a/b的余数r和b的最大公约数&#xff0c; 如果r余数为0则b为最大公约数 private static int gcd(int a ,int b){if(b 0)…

生活感悟(一)

生活中会有很多不顺心&#xff1b; 如果你的生活比较顺利&#xff0c;请珍惜&#xff0c;并不是每个人都会那么幸运&#xff1b; 感觉自己总是后知后觉&#xff1b; 但是人生只有一次是吧&#xff1b; 在人生的十字路口&#xff0c;努力做好一切&#xff1b; 可是生活总不那么近…

NAT模式下虚拟机与主机网络关系配置

哎&#xff0c;又是烦人的网络关系耽误了好长时间。 当前情况是使用NAT模式使得主机与虚拟机与外网连通 具体配置如下&#xff1a; 这是VM上配置 如下是虚拟机ip配置 vi /etc/sysconfig/network-scripts/ifcfg-ens33 vi /etc/resolv.conf service network restart

Notes of fwt

昨天考试由于不会fwt而爆炸,所以今天搞了一下fwt……话说这玩意的普及程度已经很高了.fwt,快速沃尔什变换,可以用于位运算卷积的优化,是一种线性变换,所以就会有许多好的性质(eg:可以直接模,可以修改运算等). & | ^ 的变换定义与方法是基础,在此基础上的扩展与运用是重要的…

开发中遇到的bug记录

1.启动类启动提示Mapper无法注入 启动来上注解 MapperScan(basePackages "com.xxx.xxx") 注意包路径 2.启动类启动提示datasource 的url等无法识别注入 datasource配置文件配置的druid数据源&#xff0c;注意Pom文件配置druid的pom坐标 3.datasource的bean注入…

python学习笔记-day6-函数的延续【汉字转拼音模块,函数返回多个值,匿名函数,列表生成式,generator生成器,三元运算符】...

继续来说函数的后续知识点&#xff0c;主要是函数返回多个值&#xff0c;匿名函数&#xff0c;三元运算符&#xff0c;算是比较小的知识点。 一、汉字转拼音模的使用 1、安装模块 #install xpinyin 2、如果使用 结果&#xff1a; 二、函数返回多个值 1、python的函数可以同时返…

EasyExcel项目使用

2.0.5版本做了很大改变&#xff0c;记录2.1.6的所使用的工具类及方法 其实持续对easyexcel的git进行关注是最方便的&#xff0c;上面也有完整的demo以及工具类等等 1.easyExcel的pom坐标 <!-- easyexcel --><dependency><groupId>com.alibaba</groupId&g…

c# vs2010 excel 上传oracle数据

excel 数据表上传到oracle数据库。过程例如以下&#xff1a; 1、打开本地excel文件 2、用OleDb连接excel文件 3、将来excel的数据读取到dataset中 4、把dataset 中数据insert到oracle中对应的表中 以下截图说明&#xff1a; 建立项目文件。非常easy。就是建立普通的winform项目…

SpringBoot编写sh脚本进行启停

SpringBoot项目可以使用内嵌tomcat的jar包启动方式也可以选择war包配置等等进行外置tomcat部署启动 我这里记录一下内嵌tomcat的jar包启动方式及shell脚本 maven clean package 打jar包 本地Java -jar xxx.jar启动测试&#xff0c;如果提示没有主方法入口等问题&#xff0…

Nacos配置文件覆盖问题

近期新项目上cloud alibaba架构 用的注册和配置中心就是用的nacos Nacos作为配置中心给我的感觉也是不是很好用&#xff0c;大概配置中心都不是很好用吧&#xff0c;可能用多了会好点 首先说一下遇到的问题&#xff0c; nacos有几个范围性的概念 我就不说了 1.首先是命名空…

leetCode题解之Reshape the Matrix

1、题目描述 2、分析 使用了一个队列。 3、代码 1 vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {2 3 if( nums.size() * nums[0].size() ! r * c )4 return nums;5 vecto…

项目搭建Nacos及遇到问题解决

新项目决定用SpringCloud Alibaba组件作为基础架构搭建微服务架构 Nacos作为服务注册与发现中心&#xff0c;和eurake有些区别&#xff0c;首先是CAP原则&#xff0c;eurake属于高可用AP&#xff0c;nacos属于一致性CP。 其次eurake的使用是在代码中创建项目并整合入其中&…