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. IO流_文件切割与合并(带配置信息)

    在切割文件的时候应该生成一个记录文件信息的文件,以便在以后合并文件的时候知道这个文件原来的文件名和记录文件切割完后生成了多少个切割文件 import java.io.File; import java ...

  2. cmd 监控网络状况

    提示:如果提示curl不是内部命令,请自行百度 windows 安装curl @echo off color 1f title 正在监控 echo 正在监控http://ioscheck.duapp. ...

  3. PHP中使用Elasticsearch

    PHP中使用Elasticsearch composer require elasticsearch/elasticsearch 会自动加载合适的版本!我的php是5.6的,它会自动加载5.3的ela ...

  4. Domain Adaptation (1)选题讲解

    1 所选论文 论文题目: <Unsupervised Domain Adaptation with Residual Transfer Networks> 论文信息: NIPS2016, ...

  5. ethereum/EIPs-1078 Universal login / signup using ENS subdomains

    https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1078.md eip title author discussions-to status ...

  6. css学习之样式层级和权重

    第一种情况 当选择器相同的情况下,引入方式的前后,决定页面最后的效果 ---------外部在最后面显示 ---------内部在最后面显示 第二种情况 引入方式相同时候,则是按照权重取最大(取权重最 ...

  7. Dubbo高级篇4

    https://blog.csdn.net/moonpure/article/details/52842115 线程模型 http://dubbo.io/User+Guide-zh.htm 用户指南& ...

  8. win7/10下Qt Creator调试提示:The selected debugger may be inappropriate for the inferior的解决办法

    在win7/10下Qt Creator调试提示:The selected debugger may be inappropriate for the inferior的错误提示内容如下图所示: 一般弹 ...

  9. 深入浅出的webpack构建工具---webpack基本配置(一)

    深入浅出的webpack构建工具---webpack基本配置(一) 阅读目录 一:webpack入门构建: 1. 安装webpack到全局 2. 安装webpack到本项目. 3. 如何使用webpa ...

  10. AI 线性回归

    线性回归(Linear Regression),顾名思义,输出是输入的线性函数.因为通常会附加偏置(bias)参数,所以实际是仿射函数. 参考链接: http://cs229.stanford.edu ...