当前位置: 移动技术网 > IT编程>开发语言>c# > C# DateTime.ToString根据不同语言生成相应的时间格式

C# DateTime.ToString根据不同语言生成相应的时间格式

2019年07月18日  | 移动技术网IT编程  | 我要评论
我想大家对datetime.tostring()方法的用法肯定已经非常熟悉了,但我想大家用过的大部分用法都是:datetime.tostring(“format”),不过本

我想大家对datetime.tostring()方法的用法肯定已经非常熟悉了,但我想大家用过的大部分用法都是:datetime.tostring(“format”),不过本文想讲述的是它的另一个重载方法datetime.tostring("format",iformatprovider)。

如果大家做的项目中有多语言的需求的话,那么肯定少不了会有的功能要显示一个时间,由于文差异域每种语言用户对时间格式的需求肯定是不一样的,比如英文中的时间格式是:12/2/2013 8:20:22 pm,而德语的格式是:02.12.2013 20:20:22;这个区别还是很大的,而且如果语言种数多的话,也很难对每种特定语言的时间格式作特定的处理,而且也不利于程序的扩展性。不过万幸的是微软给我们提供了cultureinfo这个类,同时这个类继承了iformatprovider这个接口,所以我们可以利用它来得到我们想要的时间格式(其它格式也可以通过它来获取,比哪货币等)。

闲话不多说,直接看代码:

thread.currentthread.currentculture = new system.globalization.cultureinfo("zh-cn");
datetime dt = convert.todatetime("2013-12-2 20:20:22");

console.writeline("当前语言:" + thread.currentthread.currentculture.displayname);
console.writeline("日期:2013-12-2 20:20:22");
console.writeline("d-短日期模式:  " + dt.tostring("d", thread.currentthread.currentculture));
console.writeline("d-长日期模式:  " + dt.tostring("d", thread.currentthread.currentculture));
console.writeline("f-完整日期/时间模式(短时间):  " + dt.tostring("f", thread.currentthread.currentculture));
console.writeline("f-完整日期/时间模式(长时间):  " + dt.tostring("f", thread.currentthread.currentculture));
console.writeline("g-常规日期/时间模式(短时间):  " + dt.tostring("g", thread.currentthread.currentculture));
console.writeline("g-常规日期/时间模式(长时间):  " + dt.tostring("g", thread.currentthread.currentculture));
console.writeline("m-月日模式(m):  " + dt.tostring("m", thread.currentthread.currentculture));
console.writeline("m-月日模式(m):  " + dt.tostring("m", thread.currentthread.currentculture));
console.writeline("o-往返日期/时间模式:  " + dt.tostring("o", thread.currentthread.currentculture));
console.writeline("r-rfc1123 模式:  " + dt.tostring("r", thread.currentthread.currentculture));
console.writeline("s-可排序的日期/时间模式;符合 iso 8601:  " + dt.tostring("s", thread.currentthread.currentculture));
console.writeline("t-短时间模式:  " + dt.tostring("t", thread.currentthread.currentculture));
console.writeline("t-长时间模式:  " + dt.tostring("t", thread.currentthread.currentculture));
console.writeline("u-通用的可排序日期/时间模式:  " + dt.tostring("u", thread.currentthread.currentculture));
console.writeline("u-通用的可排序日期/时间模式:  " + dt.tostring("u", thread.currentthread.currentculture));
console.writeline("y-年月模式:  " + dt.tostring("y", thread.currentthread.currentculture));

console.writeline(dt.tostring("yyyymmdd"));
console.readline();

在实际应用中,我们可以把第一行的“de-de“换成用户切换的语言,然后通过设置当前的线程语言为当前语言种类,那么我们就可以在程序的任一位置通过”thread.currentthread.currentculture”来获得。以前运行结果对比如下:

从结果可以知道,如果想通过cultureinfo来得到不同的语言格式,那么就不能用"yyyymmdd","yyyy-mm-dd"等这种自定义格式。

附:得到货币格式的写法为:decimal d = 123659.256m; d.tostring("c", thread.currentthread.currentculture.numberformat);

关于cultureinfo 类

cultureinfo 类保存区域性特定的信息,如关联的语言、子语言、国家/地区、日历和区域性约定。此类还提供对 datetimeformatinfo、numberformatinfo、compareinfo 和 textinfo 的区域性特定实例的访问。这些对象包含区域性特定操作(如大小写、格式化日期和数字以及比较字符串)所需的信息。

区域性名称和标识符

区域性名称遵循 rfc 1766 标准,格式为“<languagecode2>-<country/regioncode2>”,其中 <languagecode2> 是从 iso 639-1 派生的由两个小写字母构成的代码,<country/regioncode2> 是从 iso 3166 派生的由两个大写字母构成的代码。例如,美国英语为“en-us”。在双字母语言代码不可用的情况中,将使用从 iso 639-2 派生的三字母代码;例如,三字母代码“div”用于使用 dhivehi 语言的区域。某些区域性名称带有指定书写符号的后缀;例如“-cyrl”指定西里尔语书写符号,“-latn”指定拉丁语书写符号。
system.globalization 命名空间中的此类和其他类接受并使用下列预定义的区域性名称和标识符。

区域性名称

区域性标识符

语言-国家/地区

""(空字符串)

0x007f

固定区域性

af

0x0036

南非荷兰语

af-za

0x0436

南非荷兰语 - 南非

sq

0x001c

阿尔巴尼亚语

sq-al

0x041c

阿尔巴尼亚语 - 阿尔巴尼亚

ar

0x0001

阿拉伯语

ar-dz

0x1401

阿拉伯语 - 阿尔及利亚

ar-bh

0x3c01

阿拉伯语 - 巴林

ar-eg

0x0c01

阿拉伯语 - 埃及

ar-iq

0x0801

阿拉伯语 - 伊拉克

ar-jo

0x2c01

阿拉伯语 - 约旦

ar-kw

0x3401

阿拉伯语 - 科威特

ar-lb

0x3001

阿拉伯语 - 黎巴嫩

ar-ly

0x1001

阿拉伯语 - 利比亚

ar-ma

0x1801

阿拉伯语 - 摩洛哥

ar-om

0x2001

阿拉伯语 - 阿曼

ar-qa

0x4001

阿拉伯语 - 卡塔尔

ar-sa

0x0401

阿拉伯语 - 沙特阿拉伯

ar-sy

0x2801

阿拉伯语 - 叙利亚

ar-tn

0x1c01

阿拉伯语 - 突尼斯

ar-ae

0x3801

阿拉伯语 - 阿拉伯联合酋长国

ar-ye

0x2401

阿拉伯语 - 也门

hy

0x002b

亚美尼亚语

hy-am

0x042b

亚美尼亚语 - 亚美尼亚

az

0x002c

阿泽里语

az-az-cyrl

0x082c

阿泽里语(西里尔语)- 阿塞拜疆

az-az-latn

0x042c

阿泽里语(拉丁)- 阿塞拜疆

eu

0x002d

巴斯克语

eu-es

0x042d

巴斯克语 - 巴斯克地区

be

0x0023

白俄罗斯语

be-by

0x0423

白俄罗斯语 - 白俄罗斯

bg

0x0002

保加利亚语

bg-bg

0x0402

保加利亚语 - 保加利亚

ca

0x0003

加泰罗尼亚语

ca-es

0x0403

加泰罗尼亚语 - 加泰罗尼亚地区

zh-hk

0x0c04

中文 - 香港特别行政区

zh-mo

0x1404

中文 - 澳门特别行政区

zh-cn

0x0804

中文 - 中国

zh-chs

0x0004

中文(简体)

zh-sg

0x1004

中文 - 新加坡

zh-tw

0x0404

中文 - 台湾

zh-cht

0x7c04

中文(繁体)

hr

0x001a

克罗地亚语

hr-hr

0x041a

克罗地亚语 - 克罗地亚

cs

0x0005

捷克语

cs-cz

0x0405

捷克语 - 捷克共和国

da

0x0006

丹麦语

da-dk

0x0406

丹麦语 - 丹麦

div

0x0065

马尔代夫语

div-mv

0x0465

马尔代夫语 - 马尔代夫

nl

0x0013

荷兰语

nl-be

0x0813

荷兰语 - 比利时

nl-nl

0x0413

荷兰语 - 荷兰

en

0x0009

英语

en-au

0x0c09

英语 - 澳大利亚

en-bz

0x2809

英语 - 伯利兹

en-ca

0x1009

英语 - 加拿大

en-cb

0x2409

英语 - 加勒比

en-ie

0x1809

英语 - 爱尔兰

en-jm

0x2009

英语 - 牙买加

en-nz

0x1409

英语 - 新西兰

en-ph

0x3409

英语 - 菲律宾

en-za

0x1c09

英语 - 南非

en-tt

0x2c09

英语 - 特立尼达和多巴哥

en-gb

0x0809

英语 - 英国

en-us

0x0409

英语 - 美国

en-zw

0x3009

英语 - 津巴布韦

et

0x0025

爱沙尼亚语

et-ee

0x0425

爱沙尼亚语 - 爱沙尼亚

fo

0x0038

法罗语

fo-fo

0x0438

法罗语 - 法罗群岛

fa

0x0029

波斯语

fa-ir

0x0429

波斯语 - 伊朗

fi

0x000b

芬兰语

fi-fi

0x040b

芬兰语 - 芬兰

fr

0x000c

法语

fr-be

0x080c

法语 - 比利时

fr-ca

0x0c0c

法语 - 加拿大

fr-fr

0x040c

法语 - 法国

fr-lu

0x140c

法语 - 卢森堡

fr-mc

0x180c

法语 - 摩纳哥

fr-ch

0x100c

法语 - 瑞士

gl

0x0056

加利西亚语

gl-es

0x0456

加利西亚语 - 加利西亚地区

ka

0x0037

格鲁吉亚语

ka-ge

0x0437

格鲁吉亚语 - 格鲁吉亚

de

0x0007

德语

de-at

0x0c07

德语 - 奥地利

de-de

0x0407

德语 - 德国

de-li

0x1407

德语 - 列支敦士登

de-lu

0x1007

德语 - 卢森堡

de-ch

0x0807

德语 - 瑞士

el

0x0008

希腊语

el-gr

0x0408

希腊语 - 希腊

gu

0x0047

古吉拉特语

gu-in

0x0447

古吉拉特语 - 印度

he

0x000d

希伯来语

he-il

0x040d

希伯来语 - 以色列

hi

0x0039

印地语

hi-in

0x0439

印地语 - 印度

hu

0x000e

匈牙利语

hu-hu

0x040e

匈牙利语 - 匈牙利

is

0x000f

冰岛语

is-is

0x040f

冰岛语 - 冰岛

id

0x0021

印度尼西亚语

id-id

0x0421

印度尼西亚语 - 印度尼西亚

it

0x0010

意大利语

it-it

0x0410

意大利语 - 意大利

it-ch

0x0810

意大利语 - 瑞士

ja

0x0011

日语

ja-jp

0x0411

日语 - 日本

kn

0x004b

卡纳达语

kn-in

0x044b

卡纳达语 - 印度

kk

0x003f

哈萨克语

kk-kz

0x043f

哈萨克语 - 哈萨克斯坦

kok

0x0057

贡根语

kok-in

0x0457

贡根语 - 印度

ko

0x0012

朝鲜语

ko-kr

0x0412

朝鲜语 - 韩国

ky

0x0040

吉尔吉斯语

ky-kg

0x0440

吉尔吉斯语 - 吉尔吉斯坦

lv

0x0026

拉脱维亚语

lv-lv

0x0426

拉脱维亚语 - 拉脱维亚

lt

0x0027

立陶宛语

lt-lt

0x0427

立陶宛语 - 立陶宛

mk

0x002f

马其顿语

mk-mk

0x042f

马其顿语 - 前南斯拉夫联盟马其顿共和国

ms

0x003e

马来语

ms-bn

0x083e

马来语 - 文莱

ms-my

0x043e

马来语 - 马来西亚

mr

0x004e

马拉地语

mr-in

0x044e

马拉地语 - 印度

mn

0x0050

蒙古语

mn-mn

0x0450

蒙古语 - 蒙古

no

0x0014

挪威语

nb-no

0x0414

挪威语(伯克梅尔)- 挪威

nn-no

0x0814

挪威语(尼诺斯克)- 挪威

pl

0x0015

波兰语

pl-pl

0x0415

波兰语 - 波兰

pt

0x0016

葡萄牙语

pt-br

0x0416

葡萄牙语 - 巴西

pt-pt

0x0816

葡萄牙语 - 葡萄牙

pa

0x0046

旁遮普语

pa-in

0x0446

旁遮普语 - 印度

ro

0x0018

罗马尼亚语

ro-ro

0x0418

罗马尼亚语 - 罗马尼亚

ru

0x0019

俄语

ru-ru

0x0419

俄语 - 俄罗斯

sa

0x004f

梵语

sa-in

0x044f

梵语 - 印度

sr-sp-cyrl

0x0c1a

塞尔维亚语(西里尔语)- 塞尔维亚

sr-sp-latn

0x081a

塞尔维亚语(拉丁)- 塞尔维亚

sk

0x001b

斯洛伐克语

sk-sk

0x041b

斯洛伐克语 - 斯洛伐克

sl

0x0024

斯洛文尼亚语

sl-si

0x0424

斯洛文尼亚语 - 斯洛文尼亚

es

0x000a

西班牙语

es-ar

0x2c0a

西班牙语 - 阿根廷

es-bo

0x400a

西班牙语 - 玻利维亚

es-cl

0x340a

西班牙语 - 智利

es-co

0x240a

西班牙语 - 哥伦比亚

es-cr

0x140a

西班牙语 - 哥斯达黎加

es-do

0x1c0a

西班牙语 - 多米尼加共和国

es-ec

0x300a

西班牙语 - 厄瓜多尔

es-sv

0x440a

西班牙语 - 萨尔瓦多

es-gt

0x100a

西班牙语 - 危地马拉

es-hn

0x480a

西班牙语 - 洪都拉斯

es-mx

0x080a

西班牙语 - 墨西哥

es-ni

0x4c0a

西班牙语 - 尼加拉瓜

es-pa

0x180a

西班牙语 - 巴拿马

es-py

0x3c0a

西班牙语 - 巴拉圭

es-pe

0x280a

西班牙 - 秘鲁

es-pr

0x500a

西班牙语 - 波多黎各

es-es

0x0c0a

西班牙语 - 西班牙

es-uy

0x380a

西班牙语 - 乌拉圭

es-ve

0x200a

西班牙语 - 委内瑞拉

sw

0x0041

斯瓦希里语

sw-ke

0x0441

斯瓦希里语 - 肯尼亚

sv

0x001d

瑞典语

sv-fi

0x081d

瑞典语 - 芬兰

sv-se

0x041d

瑞典语 - 瑞典

syr

0x005a

叙利亚语

syr-sy

0x045a

叙利亚语 - 叙利亚

ta

0x0049

泰米尔语

ta-in

0x0449

泰米尔语 - 印度

tt

0x0044

鞑靼语

tt-ru

0x0444

鞑靼语 - 俄罗斯

te

0x004a

泰卢固语

te-in

0x044a

泰卢固语 - 印度

th

0x001e

泰语

th-th

0x041e

泰语 - 泰国

tr

0x001f

土耳其语

tr-tr

0x041f

土耳其语 - 土耳其

uk

0x0022

乌克兰语

uk-ua

0x0422

乌克兰语 - 乌克兰

ur

0x0020

乌尔都语

ur-pk

0x0420

乌尔都语 - 巴基斯坦

uz

0x0043

乌兹别克语

uz-uz-cyrl

0x0843

乌兹别克语(西里尔语)- 乌兹别克斯坦

uz-uz-latn

0x0443

乌兹别克语(拉丁)- 乌兹别克斯坦

vi

0x002a

越南语

vi-vn

0x042a

越南语 - 越南

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网