String Format for DateTime [C#]
This example shows how to format DateTime using String.Format method. All formatting can be done also using DateTime.ToString method.
Custom DateTime Formatting
There are following custom format specifiers y
(year), M
(month), d
(day), h
(hour 12), H
(hour 24),m
(minute), s
(second), f
(second fraction), F
(second fraction, trailing zeroes are trimmed), t
(P.M or A.M) and z
(time zone).
Following examples demonstrate how are the format specifiers rewritten to the output.
[C#]
// create date time 2008-03-09 16:05:07.123
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123); String.Format("{0:y yy yyy yyyy}", dt); // "8 08 008 2008" year
String.Format("{0:M MM MMM MMMM}", dt); // "3 03 Mar March" month
String.Format("{0:d dd ddd dddd}", dt); // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}", dt); // "4 04 16 16" hour 12/24
String.Format("{0:m mm}", dt); // "5 05" minute
String.Format("{0:s ss}", dt); // "7 07" second
String.Format("{0:f ff fff ffff}", dt); // "1 12 123 1230" sec.fraction
String.Format("{0:F FF FFF FFFF}", dt); // "1 12 123 123" without zeroes
String.Format("{0:t tt}", dt); // "P PM" A.M. or P.M.
String.Format("{0:z zz zzz}", dt); // "-6 -06 -06:00" time zone
You can use also date separator /
(slash) and time sepatator :
(colon). These characters will be rewritten to characters defined in the current DateTimeFormatInfo.DateSeparator andDateTimeFormatInfo.TimeSeparator.
[C#]
// date separator in german culture is "." (so "/" changes to ".")
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9/3/2008 16:05:07" - english (en-US)
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9.3.2008 16:05:07" - german (de-DE)
Here are some examples of custom date and time formatting:
[C#]
// month/day numbers without/with leading zeroes
String.Format("{0:M/d/yyyy}", dt); // "3/9/2008"
String.Format("{0:MM/dd/yyyy}", dt); // "03/09/2008" // day/month names
String.Format("{0:ddd, MMM d, yyyy}", dt); // "Sun, Mar 9, 2008"
String.Format("{0:dddd, MMMM d, yyyy}", dt); // "Sunday, March 9, 2008" // two/four digit year
String.Format("{0:MM/dd/yy}", dt); // "03/09/08"
String.Format("{0:MM/dd/yyyy}", dt); // "03/09/2008"
Standard DateTime Formatting
In DateTimeFormatInfo there are defined standard patterns for the current culture. For example property ShortTimePattern is string that contains value h:mm tt
for en-US culture and value HH:mm
for de-DE culture.
Following table shows patterns defined in DateTimeFormatInfo and their values for en-US culture. First column contains format specifiers for the String.Format method.
Specifier | DateTimeFormatInfo property | Pattern value (for en-US culture) |
---|---|---|
t |
ShortTimePattern | h:mm tt |
d |
ShortDatePattern | M/d/yyyy |
T |
LongTimePattern | h:mm:ss tt |
D |
LongDatePattern | dddd, MMMM dd, yyyy |
f |
(combination of D and t ) |
dddd, MMMM dd, yyyy h:mm tt |
F |
FullDateTimePattern | dddd, MMMM dd, yyyy h:mm:ss tt |
g |
(combination of d and t ) |
M/d/yyyy h:mm tt |
G |
(combination of d and T ) |
M/d/yyyy h:mm:ss tt |
m , M |
MonthDayPattern | MMMM dd |
y , Y |
YearMonthPattern | MMMM, yyyy |
r , R |
RFC1123Pattern | ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (*) |
s |
SortableDateTimePattern | yyyy'-'MM'-'dd'T'HH':'mm':'ss (*) |
u |
UniversalSortableDateTimePattern | yyyy'-'MM'-'dd HH':'mm':'ss'Z' (*) |
(*) = culture independent |
Following examples show usage of standard format specifiers in String.Format method and the resulting output.
[C#]
String.Format("{0:t}", dt); // "4:05 PM" ShortTime
String.Format("{0:d}", dt); // "3/9/2008" ShortDate
String.Format("{0:T}", dt); // "4:05:07 PM" LongTime
String.Format("{0:D}", dt); // "Sunday, March 09, 2008" LongDate
String.Format("{0:f}", dt); // "Sunday, March 09, 2008 4:05 PM" LongDate+ShortTime
String.Format("{0:F}", dt); // "Sunday, March 09, 2008 4:05:07 PM" FullDateTime
String.Format("{0:g}", dt); // "3/9/2008 4:05 PM" ShortDate+ShortTime
String.Format("{0:G}", dt); // "3/9/2008 4:05:07 PM" ShortDate+LongTime
String.Format("{0:m}", dt); // "March 09" MonthDay
String.Format("{0:y}", dt); // "March, 2008" YearMonth
String.Format("{0:r}", dt); // "Sun, 09 Mar 2008 16:05:07 GMT" RFC1123
String.Format("{0:s}", dt); // "2008-03-09T16:05:07" SortableDateTime
String.Format("{0:u}", dt); // "2008-03-09 16:05:07Z" UniversalSortableDateTime
String Format for DateTime [C#]的更多相关文章
- String Format for DateTime
This example shows how to format DateTime using String.Format method. All formatting can be done als ...
- c# string.format和tostring()
字符 说明 示例 输出 C 货币 string.Format("{0:C3}", 2) $2.000 D 十进制 string.Format("{0:D3}", ...
- 用DateTime.ToString(string format)输出不同格式的日期
http://www.cnblogs.com/xvqm00/archive/2009/02/19/1394093.html DateTime.ToString()函数有四个重载.一般用得多的就是不带参 ...
- 异常-----Can't convert the date to string, because it is not known which parts of the date variable are in use. Use ?date, ?time or ?datetime built-in, or ?string.\u003Cformat> or ?string(format) built-
1.错误描述 五月 27, 2014 12:07:05 上午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template proc ...
- String.Format,DateTime日期时间格式化
DateTime dt = DateTime.Now;//2010年10月4日 17点05分 string str = ""; //st ...
- [转]用DateTime.ToString(string format)输出不同格式的日期
DateTime.ToString()函数有四个重载.一般用得多的就是不带参数的那个了.殊不知,DateTime.ToString(string format)功能更强大,能输出不同格式的日期.以下把 ...
- 【转】string.Format对C#字符串格式化
转自:http://blog.csdn.net/samsone/article/details/7556781 1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) str ...
- C#中string.format用法详解
C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...
- string.Format格式化用法详解
1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) string.Format("{0:C}",0.2) 结果为:¥0.20 (英文操作系统结果:$0 ...
随机推荐
- js 实现关键词球状旋转效果
效果图 html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...
- Java遍历Map、List、Array
1.遍历array,使用for循环或者foreach(本人建议使用) int arr[] = {2, 3, 1}; System.out.println("----1----排序前的一维数组 ...
- 我的github今天大手笔分享,welcome——fork
看项目名就知道是什么样子的demo,还包含了数据库文件,welcome fork!
- 黑马程序员-ReadInt
判断输入的字符串是否为数字. namespace 读入一个整数 { class Program { static void Main(string[] args) { Console.WriteLin ...
- 解决ASP.NET中ReportView与IE11的兼容性问题
前久发现以前用ReportView开发的一个软件的报表,在IE11上运行时出错,陆续查了好几天才解决了问题. 开发环境: VS2010,ReportView 10.0.402,RDLC报表模板 问题: ...
- mac 查找当前目录下所有同一类型文件,并执行命令行
以TexturePacker举例 MAC下用TexturePacker命令行打包当前目录下所有的 *.tps文件 1.配置好tps文件需要配置好路径.参数等.(也可不配置,用命令行实现.具体参考:ht ...
- class-loader.
the jdk hierarchical relationship of class-loader ----Module Class Loading and Bootstrapping---- boo ...
- [转] NSString / NSMutableString 字符串处理,常用代码
原文 : http://justcoding.iteye.com/blog/1405951 Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString , ...
- 人民币符号¥在css和html正确显示
商城项目需要涉及到人民币的页面现实问题.但是¥(指的是通常输入法中文全角模式下按shift+4的那个)在宋体(v3.03, v5.0)的情况下是显示一杠.常见的其他字体微软雅黑(Microsoft Y ...
- javascript学习(知识点整理)
有了这个代码,就可以在定义 中增加更多的控制了 后面会举例关于extjs定义的更多控制 此种方案可以解决定义时需要一些函数调用的情况 函数作用域和声明提前: 即由于js是解释性语言,在执行前会 ...