.net 中关于日期格式摸索心得
设置程序中的时间格式有两种办法
1,通过DateTime的dateformat。
DateTime a = Convert.ToDateTime("04-05-07 12:46:53");
string aaaaa=a.ToString("yyyy-MM-dd hh:mm:ss");
2,通过设定本机的区域可以设定日期时间的格式。
到控制面板中设置,或者通过程序设置
以下从msdn上找到的
using System.Globalization;
using System.Threading;
using System.Security.Permissions;
[assembly:SecurityPermission( SecurityAction.RequestMinimum, ControlThread = true )]
public class SamplesCultureInfo {
public static void Main() {
// Displays the name of the CurrentCulture of the current thread.
Console.WriteLine( "CurrentCulture is {0}.", CultureInfo.CurrentCulture.Name );
// Changes the CurrentCulture of the current thread to th-TH.
Thread.CurrentThread.CurrentCulture = new CultureInfo( "th-TH", false );
Console.WriteLine( "CurrentCulture is now {0}.", CultureInfo.CurrentCulture.Name );
// Displays the name of the CurrentUICulture of the current thread.
Console.WriteLine( "CurrentUICulture is {0}.", CultureInfo.CurrentUICulture.Name );
// Changes the CurrentUICulture of the current thread to ja-JP.
Thread.CurrentThread.CurrentUICulture = new CultureInfo( "ja-JP", false );
Console.WriteLine( "CurrentUICulture is now {0}.", CultureInfo.CurrentUICulture.Name );
}
}
如想修改特定的知可以这样写
System.Globalization.DateTimeFormatInfo df = new System.Globalization.DateTimeFormatInfo();
System.Globalization.CultureInfo info = new CultureInfo("zh-CN");
df.DateSeparator = "?";
df.FullDateTimePattern = "MMyyyydd hh:mm:ss";
Thread.CurrentThread.CurrentCulture = info;
info.DateTimeFormat = df;
Thread.CurrentThread.CurrentCulture = info;
但在pda中却不支持修改本地区域设置,只能采用第一种方法。
浙公网安备 33010602011771号