Int.ToString(format):

格式字符串采用以下形式:Axx,其中 A 为格式说明符,指定格式化类型,xx 为精度说明符,控制格式化输出的有效位数或小数位数,具体如下:

格式说明符

说明 示例 输出
C 货币 2.5.ToString("C") ¥2.50
D 十进制数 25.ToString("D5") 00025
E 科学型 25000.ToString("E") 2.500000E+005
F 固定点 25.ToString("F2") 25.00
G 常规 2.5.ToString("G") 2.5
N 数字 2500000.ToString("N") 2,500,000.00
X 十六进制 255.ToString("X") FF

DateTime.ToString(format):

以下格式只能单独使用,表示特定的格式:

  • d ShortDatePattern
  • D LongDatePattern
  • f 完整日期和时间(长日期和短时间)
  • F FullDateTimePattern(长日期和长时间)
  • g 常规(短日期和短时间)
  • G 常规(短日期和长时间)
  • m、M MonthDayPattern
  • r、R RFC1123Pattern
  • s 使用当地时间的 SortableDateTimePattern(基于 ISO 8601)
  • t ShortTimePattern
  • T LongTimePattern
  • u UniversalSortableDateTimePattern 用于显示通用时间的格式
  • U 使用通用时间的完整日期和时间(长日期和长时间)
  • y、Y YearMonthPattern

以下格式可以组合使用,格式化出不同的日期显示格式:

  • d 月中的某一天。一位数的日期没有前导零。
  • dd 月中的某一天。一位数的日期有一个前导零。
  • ddd 周中某天的缩写名称,在 AbbreviatedDayNames 中定义。
  • dddd 周中某天的完整名称,在 DayNames 中定义。
  • M 月份数字。一位数的月份没有前导零。
  • MM 月份数字。一位数的月份有一个前导零。
  • MMM 月份的缩写名称,在 AbbreviatedMonthNames 中定义。
  • MMMM 月份的完整名称,在 MonthNames 中定义。
  • y 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示不具有前导零的年份。
  • yy 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示具有前导零的年份。
  • yyyy 包括纪元的四位数的年份。
  • gg 时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。
  • h 12 小时制的小时。一位数的小时数没有前导零。
  • hh 12 小时制的小时。一位数的小时数有前导零。
  • H 24 小时制的小时。一位数的小时数没有前导零。
  • HH 24 小时制的小时。一位数的小时数有前导零。
  • m 分钟。一位数的分钟数没有前导零。
  • mm 分钟。一位数的分钟数有一个前导零。
  • s 秒。一位数的秒数没有前导零。
  • ss 秒。一位数的秒数有一个前导零。
  • f 秒的小数精度为一位。其余数字被截断。
  • ff 秒的小数精度为两位。其余数字被截断。
  • fff 秒的小数精度为三位。其余数字被截断。
  • ffff 秒的小数精度为四位。其余数字被截断。
  • fffff 秒的小数精度为五位。其余数字被截断。
  • ffffff 秒的小数精度为六位。其余数字被截断。
  • fffffff 秒的小数精度为七位。其余数字被截断。
  • t 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项的第一个字符(如果存在)。
  • tt 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项(如果存在)。
  • z 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数没有前导零。例如,太平洋标准时间是“-8”。
  • zz 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数有前导零。例如,太平洋标准时间是“-08”。
  • zzz 完整时区偏移量(“+”或“-”后面跟有小时和分钟)。一位数的小时数和分钟数有前导零。例如,太平洋标准时间是“-08:00”。
  • : 在 TimeSeparator 中定义的默认时间分隔符。
  • / 在 DateSeparator 中定义的默认日期分隔符。
  • % c 其中 c 是格式模式(如果单独使用)。如果格式模式与原义字符或其他格式模式合并,则可以省略“%”字符。
  • " c 其中 c 是任意字符。照原义显示字符。若要显示反斜杠字符,请使用“""”。

    DateTime.ToString() 函数有四个重载。一般用得多的就是不带参数的那个了。殊不知, DateTime.ToString(string format) 功能更强大,能输出不同格式的日期。以下把一些情况罗列出来,供大家参考。有些在 MSDN 上有的就没有列出来了。

    1.           y 代表年份,注意是小写的 y ,大写的Y并不代表年份。

    2.           M 表示月份。

    3.           d 表示日期,注意 D 并不代表什么。

    4.           h 或 H 表示小时, h 用的是 12 小时制, H 用的是 24 小时制。

    5.           m 表示分钟。

    6.           s 表示秒。注意 S 并不代表什么。

格式

输出

示例

y

7

string yy = DateTime.Now.ToString("y-MM")

yy="7-05"

yy

07

string yy = DateTime.Now.ToString("yy-MM")

yy="07-05"

yyy 或更多的 y

1984

string yy = DateTime.Now.ToString("yyyy");

yy="2007"

M

5.

string mon = DateTime.Parse("1984-05-09")ToString("yyyy-M")

mon = "1984-5"

MM

05.

string mon = DateTime.Parse("1984-05-09")ToString("M M ")

mon = "05"

MMM

如果是中文版的操作系统,则会输出:五月 .

如果是英文操作系统,则输入月份前三个字母的简写:May

string mon = DateTime.Parse("2006-07-01").ToString("MMM")

英文版操作系统: Jul

中文版操作系统:七月

MMMM 或更多的M

如果是中文版的操作系统,则会输出:五月 .

如果是英文操作系统,则输入月份的全写

string mon = DateTime.Parse("2006-07-01").ToString("MMM")

英文版操作系统: July

中文版操作系统:七月

日期或星期

d

9

string dd= DateTime.Parse("1984-05-09")ToString("d")

dd= "9"

dd

09

string dd= DateTime.Parse("1984-05-09")ToString("dd")

dd= "09"

ddd

如果是中文版的操作系统,则会输出星期,如星期三。 .

如果是英文操作系统,则输出星期的简写:如

Wed

string dd = DateTime.Parse("2006-07-01").ToString("ddd")

英文版操作系统: Wed

中文版操作系统:星期三

dddd 或更多的 d

如果是中文版的操作系统,则会输出星期,如星期三。 .

如果是英文操作系统,则输出星期:如

Wednesday

string dd = DateTime.Parse("2006-07-01").ToString("dddd")

英文版操作系统: Wednesday

中文版操作系统:星期三

小时

h

小时范围: 1-12

string hh = DateTime.Now.ToString(“h”);

hh = 8

hh 或更多的 h

小时范围: 1-12

string hh = DateTime.Now.ToString(“hh”);

hh = 08

H

小时范围: 0-23

string hh = DateTime.Now.ToString(“yyyy-H”);

hh = 2006-8

HH 或更多的 H

小时范围: 0-23

string hh = DateTime.Now.ToString(“yyyy-HH”);

hh = 2006-08

string hh = DateTime.Pare(“2006-7-4 18:00:00”).ToString(“yyyy-HH”);

hh = 2006-18

分钟

m

6

string mm = DateTime.Now.ToString("yyyy-MM-dd-m");

mm = “2006-07-01-6”;

mm 或更多的 m

06

string mm = DateTime.Now.ToString("yyyy-MM-dd-mm");

mm = “2006-07-01-06”;

s

6

string mm = DateTime.Now.ToString("yyyy-MM-dd-s");

mm = “2006-07-01-6”;

ss 或更多的 s

06

string mm = DateTime.Now.ToString("yyyy-MM-dd-ss");

mm = “2006-07-01-06”;

在String.Format中使用格式化:

在String.Format方法中也可以格式化这些结构对象,这些格式化的格式包含在“{}”之间,用“:”隔开。“:”前面为String.Format的索引值,和一般格式化一样,而“:”后面则是这些结构的格式化类型。如果使用占位符,则和索引之间使用“,”分割,正数表示右对齐,负数表示左对齐,而绝对值表示所占的字符宽度数,例如:

string.Format("the value is {0,7:f3}",123.45);

//结果 the value is 123.450

其中:7表示占位符(右对齐),占7位,如果使用-7则表示左对齐,占七位。

部分内容转载自:

http://www.cnblogs.com/huangting2009/archive/2009/02/09/1386596.html

 
 
 
 

:first-child { margin-top: 0; } blockquote > :last-child { margin-bottom: 0; } img { border: 0; max-width: 100%; height: auto !important; margin: 2px 0; } table { border-collapse: collapse; border: 1px solid #bbbbbb; } td, th { padding: 4px 8px; border-collapse: collapse; border: 1px solid #bbbbbb; } @media only screen and (-webkit-max-device-width: 1024px), only screen and (-o-max-device-width: 1024px), only screen and (max-device-width: 1024px), only screen and (-webkit-min-device-pixel-ratio: 3), only screen and (-o-min-device-pixel-ratio: 3), only screen and (min-device-pixel-ratio: 3) { html, body { font-size: 17px; } body { line-height: 1.7; padding: 0.75rem 0.9375rem; color: #353c47; } h1 { font-size: 2.125rem; } h2 { font-size: 1.875rem; } h3 { font-size: 1.625rem; } h4 { font-size: 1.375rem; } h5 { font-size: 1.125rem; } h6 { color: inherit; } ul, ol { padding-left: 2.5rem; } blockquote { padding: 0 0.9375rem; } }
-->

div{font-size:15px;}.wiz-table-tools .wiz-table-menu-item.active .wiz-table-menu-sub {display: block}.wiz-table-tools .wiz-table-menu-sub:before, .wiz-table-tools .wiz-table-menu-sub:after {position: absolute;content: " ";border-style: solid;border-color: transparent;border-bottom-color: #cccccc;left: 22px;margin-left: -14px;top: -8px;border-width: 0 8px 8px 8px;z-index:10;}.wiz-table-tools .wiz-table-menu-sub:after {border-bottom-color: #ffffff;top: -7px;}.wiz-table-tools .wiz-table-menu-sub-item {padding: 4px 12px;font-size: 14px;}.wiz-table-tools .wiz-table-menu-sub-item.split {border-top: 1px solid #E0E0E0;}.wiz-table-tools .wiz-table-menu-sub-item:hover {background-color: #ececec;}.wiz-table-tools .wiz-table-menu-sub-item.disabled {color: #bbbbbb;cursor: default;}.wiz-table-tools .wiz-table-menu-sub-item.disabled:hover {background-color: transparent;}.wiz-table-tools .wiz-table-menu-item.wiz-table-cell-bg:hover .wiz-table-color-pad {display: block;}.wiz-table-tools .wiz-table-color-pad {display: none;padding: 10px;box-sizing: border-box;width: 85px;height: 88px;background-color: #fff;cursor: default;}.wiz-table-tools .wiz-table-color-pad > div{font-size:15px;}.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item {display: inline-block;width: 15px;height: 15px;margin-right: 9px;position: relative;}.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item i.pad-demo {position: absolute;top:3px;left:0;}.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item .icon-oblique_line{color: #cc0000;}.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item:last-child {margin-right: 0;}.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item.active i.editor-icon.icon-box {color: #448aff;}.wiz-table-tools .wiz-table-cell-align {display: none;padding: 10px;box-sizing: border-box;width: 85px;height: 65px;background-color: #fff;cursor: default;}.wiz-table-tools .wiz-table-cell-align .wiz-table-cell-align-item {display: inline-block;width: 15px;height: 15px;margin-right: 9px;position: relative;}.wiz-table-tools .wiz-table-cell-align .wiz-table-cell-align-item:last-child {margin-right:0}.wiz-table-tools .wiz-table-cell-align .wiz-table-cell-align-item i.valign{position: absolute;top:3px;left:0;color: #d2d2d2;}.wiz-table-tools .wiz-table-cell-align-item.active i.editor-icon.valign {color: #a1c4ff;}.wiz-table-tools .wiz-table-cell-align-item.active i.editor-icon.icon-box,.wiz-table-tools .wiz-table-cell-align-item.active i.editor-icon.align {color: #448aff;}.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item:last-child,.wiz-table-tools .wiz-table-cell-align .wiz-table-cell-align-item:last-child {margin-right: 0;}th.wiz-selected-cell-multi, td.wiz-selected-cell-multi {background: rgba(0,102,255,.05);}th:before,td:before,#wiz-table-col-line:before,#wiz-table-range-border_start_right:before,#wiz-table-range-border_range_right:before{content: " ";position: absolute;top: 0;bottom: 0;right: -5px;width: 9px;cursor: col-resize;background: transparent;z-index:100;}th:after,td:after,#wiz-table-row-line:before,#wiz-table-range-border_start_bottom:before,#wiz-table-range-border_range_bottom:before{content: " ";position: absolute;left: 0;right: 0;bottom: -5px;height: 9px;cursor: row-resize;background: transparent;z-index:100;}.wiz-table-container {}.wiz-table-body {position:relative;padding:0 0 10px;overflow-x:auto;overflow-y:hidden;-webkit-overflow-scrolling:touch;}.wiz-table-body table {margin:0;outline:none;}td,th {height:28px;word-break:break-all;box-sizing:border-box;outline:none;}body pre.prettyprint {padding:0;}body pre.prettyprint code {white-space: pre;}body pre.prettyprint.linenums {box-shadow:none; overflow: auto;-webkit-overflow-scrolling: touch;}body pre.prettyprint.linenums ol.linenums {box-shadow: 40px 0 0 #FBFBFC inset, 41px 0 0 #ECECF0 inset; padding: 10px 10px 10px 40px !important;}
-->

C#-ToString格式化的更多相关文章

  1. C# .ToString() 格式化

    c# ToString() 格式化字符串  格式化数值:有时,我们可能需要将数值以一定的格式来呈现,就需要对数值进行格式化.我们使用格式字符串指定格式.格式字符串采用以下形式:Axx,其中 A 为格式 ...

  2. DateTime.ToString格式化日期,使用DateDiff方法获取日期时间的间隔数

    一:DateTime.ToString格式化日期 二:代码 using System; using System.Collections.Generic; using System.Component ...

  3. ToString() 格式化

    c# ToString() 格式化字符串  格式化数值:有时,我们可能需要将数值以一定的格式来呈现,就需要对数值进行格式化.我们使用格式字符串指定格式.格式字符串采用以下形式:Axx,其中 A 为格式 ...

  4. asp.net ToString() 格式化字符串

    c# ToString() 格式化字符串  格式化数值:有时,我们可能需要将数值以一定的格式来呈现,就需要对数值进行格式化.我们使用格式字符串指定格式.格式字符串采用以下形式:Axx,其中 A 为格式 ...

  5. C# Tostring 格式化输出字符串全解

    C 货币 2.5.ToString("C") ¥2.50 D 十进制数 .ToString("D5") E 科学型 .ToString("E" ...

  6. .tostring()格式化大全

    C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString("D5") 00025 E 科学型 25000.ToString ...

  7. tostring格式化输出

    C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString("D5") 00025 E 科学型 25000.ToString ...

  8. C# tostring 格式化输出 (转)

    C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString("D5") 00025 E 科学型 25000.ToString ...

  9. C#中string.Format()和ToString()格式化方法

    C#数字格式化输出是我们在编程中经常需要处理的事情,那么这里向你介绍了一些C#数字格式化输出的例子,这样就会方便你来选择和比较,什么方式是比较适合自己项目的. int a = 12345678; C# ...

  10. [C#]ToString("##")格式化用法案例一:自动编码单据流水码

    之前的写的自动编码单据流水码是写在存储过程或者函数中的,今天用程序写一个发现TOSTRING可以格式化方便. /// <summary> /// 年月日+两位流水码 /// </su ...

随机推荐

  1. 5、爬虫系列之scrapy框架

    一 scrapy框架简介 1 介绍 (1) 什么是Scrapy? Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架,非常出名,非常强悍.所谓的框架就是一个已经被集成了各种功能(高性能 ...

  2. Django之Form进阶

    s6day76 内容回顾: Form组件:  - 类  继承Form  - 字段  字段  - 实例化  - is_valid()  - cleaned_data  - errors Form 组件: ...

  3. js中typeof与instanceof用法小记

    今天写JS代码,遇到动态生成多个名称相同的input复选按钮 需要判断其是否是数组,用到了if (typeof(document.MapCheckMgr.checkid)!="undefin ...

  4. (3)HomeAssistant 连接MQTT

    整体说明 1 自己在阿里云上搭建MQTT服务器 2 自己笔记本电脑windos10搭建HASS,配置参数连接阿里云服务器 3 手机下载MQTT调试助手,当测试端 4手机当终端---阿里云MQTT--- ...

  5. ethereum/EIPs-607 Hardfork Meta: Spurious Dragon硬分叉相关

    eip title author type status created requires 607 Hardfork Meta: Spurious Dragon Alex Beregszaszi Me ...

  6. VsCode之使用WebView通信

    之前我在这篇文章VsCode插件开发之插件初步通信 通过插件完成通信,这回我还是通过插件,只不过方式主要以在ts文件里面使用webview来进行通信. 另外在此声明,一定要好好看仔细看官方文档,国内关 ...

  7. [转]matplotlib - plt.rcParams、matshow/cmap/坐标轴设置

    转自:https://blog.csdn.net/qq_30815237/article/details/87903024 1.plt.rcParams plt(matplotlib.pyplot)使 ...

  8. mac修改本机mysql的root密码

    今天同事的MAC上的mysql的登陆密码忘记了,问我是否能解决,呵呵   我查了下    并做个记录 1. 在系统偏好设置中关闭 mysql : Stop MySQL Server 2.打开终端 进入 ...

  9. Redis 安装部署

    1. 官网(https://redis.io/download)下载稳定版安装包 3.0.7或3.2或4.1; 2. 复制到部署服务器 /opt/redis4,解压 tar zxvf redis-4. ...

  10. 学习CSS布局 - 盒模型

    盒模型 在我们讨论宽度的时候,我们应该讲下与它相关的另外一个重点知识:盒模型. 当你设置了元素的宽度,实际展现的元素却超出你的设置: 这是因为元素的边框和内边距会撑开元素. 看下面的例子,两个相同宽度 ...