首先,日期時間轉換在 Asp.net 中的幾種應用:
1. <ASP:BOUNDCOLUMN DATAFIELD= "JoinTime " DATAFORMATSTRING= "{0:yyyy-MM-dd} " > <ITEMSTYLE WIDTH= "18% " > </ITEMSTYLE > </ASP:BOUNDCOLUMN >
2. e.Item.Cell[0].Text = Convert.ToDateTime(e.Item.Cell[0].Text).ToShortDateString();
3. String.Format( "yyyy-MM-dd ",yourDateTime);
4. Convert.ToDateTime("2005-8-23").ToString("yyMMdd",System.Globalization.DateTimeFormatInfo.InvariantInfo);
5. <asp:BoundColumn DataField="收款時間" HeaderText="收款時間"
DataFormatString="{0:yyyy-mm-dd}"> </asp:BoundColumn>
6. DateTime.Now.ToString("yyyyMMddhhmmss")
7. DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss")
8. 只顯示年月
DataBinder.Eval(Container.DataItem,"starttime","{0:yyyy-M}")
9. <asp:BoundColumn DataField="收款時間" HeaderText="收款時間"
DataFormatString="{0:yyyy-MM-dd HH24:mm:ss}"> </asp:BoundColumn>
其次,系統中有效時區列表:
1. 根據本地時間取得時區列表:
DateTimeOffset chinaDate = DateTimeOffset.Now; //本地目前時間
Response.Write(chinaDate.ToString() + "<br/>");
ReadOnlyCollection<TimeZoneInfo> zones = TimeZoneInfo.GetSystemTimeZones();//系統中地時區識別欄位表
foreach (TimeZoneInfo timeZoneInfo in zones)
{
//通過本地時間取得格林威治標準時間,並通過這個標準時間取得不同時區ID的名稱及它的相應時間
DateTimeOffset easternDate = TimeZoneInfo.ConvertTime(chinaDate.UtcDateTime,
TimeZoneInfo.FindSystemTimeZoneById(timeZoneInfo.Id));
Response.Write(timeZoneInfo.Id + ": "
+ easternDate.ToString() + "<br/>");
}
所得結果如下:
2008-9-12 14:33:12 +08:00
Greenwich Standard Time: 2008-9-12 6:33:12 +08:00
GMT Standard Time: 2008-9-12 7:33:12 +08:00
W. Central Africa Standard Time: 2008-9-12 7:33:12 +08:00
Romance Standard Time: 2008-9-12 8:33:12 +08:00
Central European Standard Time: 2008-9-12 8:33:12 +08:00
Central Europe Standard Time: 2008-9-12 8:33:12 +08:00
W. Europe Standard Time: 2008-9-12 8:33:12 +08:00
South Africa Standard Time: 2008-9-12 8:33:12 +08:00
E. Europe Standard Time: 2008-9-12 9:33:12 +08:00
Egypt Standard Time: 2008-9-12 9:33:12 +08:00
Israel Standard Time: 2008-9-12 8:33:12 +08:00
FLE Standard Time: 2008-9-12 9:33:12 +08:00
GTB Standard Time: 2008-9-12 9:33:12 +08:00
E. Africa Standard Time: 2008-9-12 9:33:12 +08:00
Arabic Standard Time: 2008-9-12 10:33:12 +08:00
Arab Standard Time: 2008-9-12 9:33:12 +08:00
Russian Standard Time: 2008-9-12 10:33:12 +08:00
Iran Standard Time: 2008-9-12 11:03:12 +08:00
Caucasus Standard Time: 2008-9-12 11:33:12 +08:00
Arabian Standard Time: 2008-9-12 10:33:12 +08:00
Afghanistan Standard Time: 2008-9-12 11:03:12 +08:00
West Asia Standard Time: 2008-9-12 11:33:12 +08:00
Ekaterinburg Standard Time: 2008-9-12 12:33:12 +08:00
India Standard Time: 2008-9-12 12:03:12 +08:00
Nepal Standard Time: 2008-9-12 12:18:12 +08:00
Sri Lanka Standard Time: 2008-9-12 12:33:12 +08:00
N. Central Asia Standard Time: 2008-9-12 13:33:12 +08:00
Central Asia Standard Time: 2008-9-12 12:33:12 +08:00
Myanmar Standard Time: 2008-9-12 13:03:12 +08:00
North Asia Standard Time: 2008-9-12 14:33:12 +08:00
SE Asia Standard Time: 2008-9-12 13:33:12 +08:00
North Asia East Standard Time: 2008-9-12 15:33:12 +08:00
China Standard Time: 2008-9-12 14:33:12 +08:00
Taipei Standard Time: 2008-9-12 14:33:12 +08:00
Singapore Standard Time: 2008-9-12 14:33:12 +08:00
W. Australia Standard Time: 2008-9-12 14:33:12 +08:00
Tokyo Standard Time: 2008-9-12 15:33:12 +08:00
Korea Standard Time: 2008-9-12 15:33:12 +08:00
Yakutsk Standard Time: 2008-9-12 16:33:12 +08:00
AUS Central Standard Time: 2008-9-12 16:03:12 +08:00
Cen. Australia Standard Time: 2008-9-12 16:03:12 +08:00
West Pacific Standard Time: 2008-9-12 16:33:12 +08:00
AUS Eastern Standard Time: 2008-9-12 16:33:12 +08:00
E. Australia Standard Time: 2008-9-12 16:33:12 +08:00
Vladivostok Standard Time: 2008-9-12 17:33:12 +08:00
Tasmania Standard Time: 2008-9-12 16:33:12 +08:00
Central Pacific Standard Time: 2008-9-12 17:33:12 +08:00
New Zealand Standard Time: 2008-9-12 18:33:12 +08:00
Fiji Standard Time: 2008-9-12 18:33:12 +08:00
Tonga Standard Time: 2008-9-12 19:33:12 +08:00
Azores Standard Time: 2008-9-12 6:33:12 +08:00
Cape Verde Standard Time: 2008-9-12 5:33:12 +08:00
Mid-Atlantic Standard Time: 2008-9-12 5:33:12 +08:00
E. South America Standard Time: 2008-9-12 3:33:12 +08:00
SA Eastern Standard Time: 2008-9-12 3:33:12 +08:00
Greenland Standard Time: 2008-9-12 4:33:12 +08:00
Newfoundland Standard Time: 2008-9-12 4:03:12 +08:00
SA Western Standard Time: 2008-9-12 2:33:12 +08:00
Pacific SA Standard Time: 2008-9-12 2:33:12 +08:00
Atlantic Standard Time: 2008-9-12 3:33:12 +08:00
Eastern Standard Time: 2008-9-12 2:33:12 +08:00
US Eastern Standard Time: 2008-9-12 1:33:12 +08:00
SA Pacific Standard Time: 2008-9-12 1:33:12 +08:00
Central America Standard Time: 2008-9-12 0:33:12 +08:00
Central Standard Time: 2008-9-12 1:33:12 +08:00
Canada Central Standard Time: 2008-9-12 0:33:12 +08:00
Mexico Standard Time: 2008-9-12 1:33:12 +08:00
US Mountain Standard Time: 2008-9-11 23:33:12 +08:00
Mountain Standard Time: 2008-9-12 0:33:12 +08:00
Mexico Standard Time 2: 2008-9-12 0:33:12 +08:00
Pacific Standard Time: 2008-9-11 23:33:12 +08:00
Alaskan Standard Time: 2008-9-11 22:33:12 +08:00
Hawaiian Standard Time: 2008-9-11 20:33:12 +08:00
Samoa Standard Time: 2008-9-11 19:33:12 +08:00
Dateline Standard Time: 2008-9-11 18:33:12 +08:00
其次,取得用戶端時區位移量(javascript):
var zone = new Date().getTimezoneOffset()/60 * -1;
最後,取得在用戶端應正確顯示的時間:
//serverZoneID 為資料庫所在時區的ID,例如上面列表中的:“Eastern Standard Time”
DateTimeOffset serverDateTime = TimeZoneInfo.ConvertTime(
databaseDateTime, TimeZoneInfo.FindSystemTimeZoneById(serverZoneID));
//zone 表示用戶端時區位移量
serverDateTime.UtcDateTime.AddHours(zone);