|
|
|
ตัวอย่างการทำงานของ DateTime.TryParse ในลักษณะต่างๆ |
|
|
|
|
|
|
|
Code (C#)
var sb = new StringBuilder();
sb.AppendFormat("TimeZone: {0}\r\n\r\n", TimeZone.CurrentTimeZone.StandardName);
DoExample(sb);
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("th-TH");
DoExample(sb);
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
DoExample(sb);
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-GB");
DoExample(sb);
private void DoExample(StringBuilder sb)
{
sb.AppendFormat("CurrentCulture: {0}\r\n", Thread.CurrentThread.CurrentCulture);
sb.AppendFormat("CurrentUICulture: {0}\r\n", Thread.CurrentThread.CurrentUICulture);
var s = "2020-02-01 00:00:00";
var dt = DateTime.Parse(s);
sb.AppendFormat("{0} => {1:ddd d MMM yyyy HH:mm} Kind {2}\r\n", s, dt, dt.Kind);
s = "2020-02-01T00:00:00";
dt = DateTime.Parse(s);
sb.AppendFormat("{0} => {1:ddd d MMM yyyy HH:mm} Kind {2}\r\n", s, dt, dt.Kind);
s = "2020-02-01T00:00:00Z";
dt = DateTime.Parse(s);
sb.AppendFormat("{0} => {1:ddd d MMM yyyy HH:mm} Kind {2}\r\n", s, dt, dt.Kind);
s = "2020-02-01T00:00:00+01:00";
dt = DateTime.Parse(s);
sb.AppendFormat("{0} => {1:ddd d MMM yyyy HH:mm} Kind {2}\r\n", s, dt, dt.Kind);
s = "2020-02-01T00:00:00+07:00";
dt = DateTime.Parse(s);
sb.AppendFormat("{0} => {1:ddd d MMM yyyy HH:mm} Kind {2}\r\n", s, dt, dt.Kind);
sb.AppendLine();
}
SE Asia Standard Time คือ TimeZone +07:00
1. CurrentUICulture ไม่มีผลต่อ DateTime.TryParse
2. DateTimeString ไม่มีตัวอักษร T -> CurrentCulture มีผลต่อ TryParse
3. DateTimeString มีตัวอักษร T -> ปีจะเป็นปี ค.ศ. เสมอ
4. หากมีการระบุ TimeZone ใน DateTimeString เช่น Z (Zero), +01:00, +07:00 TryParse จะ Convert เวลาให้ไปอยู่ใน CurrentTimeZone และ DateTimeKind จะเป็น Local
Tag : .NET, C#
|
ประวัติการแก้ไข 2020-01-24 20:48:27
|
|
|
|
|
Date :
2020-01-24 20:34:42 |
By :
watcharop |
View :
1062 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตัวอย่าง DateTime.TryParse เพิ่มเติมและผลการใช้ ToUniversalTime, ToLocalTime
Code (C#)
sb.AppendFormat("CurrentCulture: {0}\r\n", Thread.CurrentThread.CurrentCulture);
sb.AppendFormat("CurrentUICulture: {0}\r\n", Thread.CurrentThread.CurrentUICulture);
sb.AppendFormat("CurrentTime: {0:ddd d MMM yyyy HH:mm}\r\n", DateTime.Now);
var s = "2/3";
var dt = DateTime.Parse(s);
sb.AppendFormat("\r\n{0} => {1:ddd d MMM yyyy HH:mm} Kind {2}\r\n", s, dt, dt.Kind);
sb.AppendFormat("ToUniversalTime => {1:ddd d MMM yyyy HH:mm} Kind {2}\r\n", s, dt.ToUniversalTime(), dt.ToUniversalTime().Kind);
sb.AppendFormat("ToLocalTime => {1:ddd d MMM yyyy HH:mm} Kind {2}\r\n", s, dt.ToLocalTime(), dt.ToLocalTime().Kind);
s = "2/3/2563";
dt = DateTime.Parse(s);
sb.AppendFormat("\r\n{0} => {1:ddd d MMM yyyy HH:mm} Kind {2}\r\n", s, dt, dt.Kind);
sb.AppendFormat("ToUniversalTime => {1:ddd d MMM yyyy HH:mm} Kind {2}\r\n", s, dt.ToUniversalTime(), dt.ToUniversalTime().Kind);
sb.AppendFormat("ToLocalTime => {1:ddd d MMM yyyy HH:mm} Kind {2}\r\n", s, dt.ToLocalTime(), dt.ToLocalTime().Kind);
s = "2/3/2019";
dt = DateTime.Parse(s);
sb.AppendFormat("\r\n{0} => {1:ddd d MMM yyyy HH:mm} Kind {2}\r\n", s, dt, dt.Kind);
sb.AppendFormat("ToUniversalTime => {1:ddd d MMM yyyy HH:mm} Kind {2}\r\n", s, dt.ToUniversalTime(), dt.ToUniversalTime().Kind);
sb.AppendFormat("ToLocalTime => {1:ddd d MMM yyyy HH:mm} Kind {2}\r\n", s, dt.ToLocalTime(), dt.ToLocalTime().Kind);
|
ประวัติการแก้ไข 2020-01-24 21:09:38
|
|
|
|
Date :
2020-01-24 20:59:09 |
By :
watcharop |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DateTimeKind ไม่มีผลต่อเมื่อเอาเวลา 2 ตัวมาลบกัน แต่มีผลกับฟั่งก์ชั่น ToUniversalTime, ToLocalTime
Code (C#)
sb.AppendFormat("CurrentCulture: {0}\r\n", Thread.CurrentThread.CurrentCulture);
sb.AppendFormat("CurrentUICulture: {0}\r\n", Thread.CurrentThread.CurrentUICulture);
var dt = DateTime.Now;
var dtUtc = dt.ToUniversalTime();
var dtLocal = dt.ToLocalTime();
sb.AppendFormat("\r\ndt: {0:ddd d MMM yyyy HH:mm}, {1}\r\n", dt, dt.Kind);
sb.AppendFormat("ToUniversalTime => {0:ddd d MMM yyyy HH:mm}, {1}\r\n", dtUtc, dtUtc.Kind);
sb.AppendFormat("ToLocalTime => {0:ddd d MMM yyyy HH:mm}, {1}\r\n", dtLocal, dtLocal.Kind);
sb.AppendFormat("dt - dtUtc => {0}\r\n", dt.Subtract(dtUtc).TotalMinutes);
sb.AppendFormat("dt - dtLocal => {0}\r\n", dt.Subtract(dtLocal).TotalMinutes);
dt = new DateTime(2020, 01, 24);
dtUtc = dt.ToUniversalTime();
dtLocal = dt.ToLocalTime();
sb.AppendFormat("\r\ndt: {0:ddd d MMM yyyy HH:mm}, {1}\r\n", dt, dt.Kind);
sb.AppendFormat("ToUniversalTime => {0:ddd d MMM yyyy HH:mm}, {1}\r\n", dtUtc, dtUtc.Kind);
sb.AppendFormat("ToLocalTime => {0:ddd d MMM yyyy HH:mm}, {1}\r\n", dtLocal, dtLocal.Kind);
sb.AppendFormat("dt - dtUtc => {0}\r\n", dt.Subtract(dtUtc).TotalMinutes);
sb.AppendFormat("dt - dtLocal => {0}\r\n", dt.Subtract(dtLocal).TotalMinutes);
dt = new DateTime(2020, 01, 24, 0, 0, 0, DateTimeKind.Utc);
dtUtc = dt.ToUniversalTime();
dtLocal = dt.ToLocalTime();
sb.AppendFormat("\r\ndt: {0:ddd d MMM yyyy HH:mm}, {1}\r\n", dt, dt.Kind);
sb.AppendFormat("ToUniversalTime => {0:ddd d MMM yyyy HH:mm}, {1}\r\n", dtUtc, dtUtc.Kind);
sb.AppendFormat("ToLocalTime => {0:ddd d MMM yyyy HH:mm}, {1}\r\n", dtLocal, dtLocal.Kind);
sb.AppendFormat("dt - dtUtc => {0}\r\n", dt.Subtract(dtUtc).TotalMinutes);
sb.AppendFormat("dt - dtLocal => {0}\r\n", dt.Subtract(dtLocal).TotalMinutes);
|
|
|
|
|
Date :
2020-01-24 21:22:54 |
By :
watcharop |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
แจ่ม
|
|
|
|
|
Date :
2020-01-24 22:05:11 |
By :
Chaidhanan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|