全球化(8):日期格式设置

     日期格式设置在整世界上并不是完全一致的。虽然每种日期格式都基本都显示年、月、日的内容。但其显示顺序和分割符却有很多的区别。相同国际/地区内的各区域之间也可能存在诸多差异。

     基本日期格式(长日期和短日期):

长日期:2010年12月20日

短日期:2010-12-20

     简单示例:

   1: using System;
   2: using System.Globalization;
   3: using System.Diagnostics;
   4: using System.Threading;
   5:  
   6: class Program
   7: {
   8:     static void Main(string[] args)
   9:     {
  10:         Debug.WriteLine("中文的长日期和短日期");
  11:         Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-CN");
  12:         ShowDate();
  13:  
  14:         Debug.WriteLine("英语的长日期和短日期");
  15:         Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
  16:         ShowDate();
  17:  
  18:         Debug.WriteLine("法语的长日期和短日期");
  19:         Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
  20:         ShowDate();
  21:  
  22:         Debug.WriteLine("西班牙的长日期和短日期");
  23:         Thread.CurrentThread.CurrentCulture = new CultureInfo("es-ES");
  24:         ShowDate();
  25:  
  26:         Console.Read();
  27:     }
  28:  
  29:     static void ShowDate()
  30:     {
  31:         Debug.WriteLine("长日期:"+DateTime.Now.ToLongDateString());
  32:         Debug.WriteLine("短日期:" + DateTime.Now.ToShortDateString());
  33:     }
  34: }

      显示结果:

中文的长日期和短日期
长日期:星期一 2010年12月20日
短日期:2010-12-20
英语的长日期和短日期
长日期:Monday, December 20, 2010
短日期:12/20/2010
法语的长日期和短日期
长日期:lundi 20 décembre 2010
短日期:20/12/2010
西班牙的长日期和短日期
长日期:lunes, 20 de diciembre de 2010
短日期:20/12/2010

      在 .NET 环境中设置日期格式的最简单有效的方法是利用DateTime 结构,利用它提供的方法您可以对DateTime 执行区分文化的操作。使用DateTimeFormatInfo类基于特定于文化的标准来格式化和显示DateTime     

      DateTimeFormatInfo 定义如何根据地域设置 DateTime 值的格式并进行显示。例如,使用 ShortDatePattern,日期 April 24, 2001 在 "en-US" 文化中被格式化为 4/24/2001,在 "en-GB" 英语(英国)文化中被格式化为 24/04/2001。DateTimeFormatInfo 实例可以针对特定文化而不是针对中立文化加以创建。中立文化没有为显示正确的日期格式提供足够的信息。正如您刚刚看到的,虽然英语(美国)和英语(英国)共享相同的中立文化(英语),但其日期格式设置却完全不同。

格式字符

关联属性/说明

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

中文的日期格式示例:

   1: using System;
   2: using System.Globalization;
   3: using System.Diagnostics;
   4: using System.Threading;
   5:  
   6: class Program
   7: {
   8:     static void Main(string[] args)
   9:     {
  10:         DateTime dt = DateTime.Now;
  11:         Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-CN");
  12:  
  13:         Console.WriteLine("Format {0}:{1}","d",dt.ToString("d"));
  14:         Console.WriteLine("Format {0}:{1}", "D", dt.ToString("D"));
  15:         Console.WriteLine("Format {0}:{1}", "f", dt.ToString("f"));
  16:         Console.WriteLine("Format {0}:{1}", "F", dt.ToString("F"));
  17:         Console.WriteLine("Format {0}:{1}", "g", dt.ToString("g"));
  18:         Console.WriteLine("Format {0}:{1}", "G", dt.ToString("G"));
  19:         Console.WriteLine("Format {0}:{1}", "m", dt.ToString("m"));
  20:         Console.WriteLine("Format {0}:{1}", "r", dt.ToString("r"));
  21:         Console.WriteLine("Format {0}:{1}", "s", dt.ToString("s"));
  22:         Console.WriteLine("Format {0}:{1}", "t", dt.ToString("t"));
  23:         Console.WriteLine("Format {0}:{1}", "T", dt.ToString("T"));
  24:         Console.WriteLine("Format {0}:{1}", "u", dt.ToString("u"));
  25:         Console.WriteLine("Format {0}:{1}", "U", dt.ToString("U"));
  26:         Console.WriteLine("Format {0}:{1}", "y", dt.ToString("y"));
  27:         Console.WriteLine("Format {0}:{1}", "Y", dt.ToString("Y"));
  28:         Console.Read();
  29:     }
  30:  
  31: }

运行结果:

image

转载于:https://www.cnblogs.com/zhaozhan/archive/2010/12/20/1912208.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值