关键字:DateTime.Parse(thistime).ToString("D", DateTimeFormatInfo.InvariantInfo)
解释:thistime:要转换的时间字符串变量。
D:格式字符串,下面列出了18种格式。
DateTimeFormatInfo.InvariantInfo:不依赖于区域性的日期属性
例:
Page
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4<html xmlns="http://www.w3.org/1999/xhtml">
5<head runat="server">
6 <title>英时间</title>
7</head>
8<body>
9 <form id="form1" runat="server">
10 <div>
11 当前时间:<br />
12 <asp:Label ID="lbThisTime" runat="server"></asp:Label><br />
13 <br />
14 转换后时间:<br />
15 <span style="text-decoration: underline">
16 <asp:Label ID="lbChangeTime" runat="server"></asp:Label><br />
17 <br />
18 <asp:Button ID="btnChange" runat="server" Text="转换" OnClick="btnChange_Click" />
19 </span>
20 </div>
21 </form>
22</body>
23</html>
24
Code
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Globalization;
12
13public partial class _Default : System.Web.UI.Page
14{
15 protected void Page_Load(object sender, EventArgs e)
16 {
17
18 }
19 protected void btnChange_Click(object sender, EventArgs e)
20 {
21 string thistime = DateTime.Now.ToString();
22 this.lbThisTime.Text = thistime;
23 this.lbChangeTime.Text = "D: " + DateTime.Parse(thistime.ToString()).ToString("D", DateTimeFormatInfo.InvariantInfo) + " "
24 + "d: " + DateTime.Parse(thistime).ToString("d", DateTimeFormatInfo.InvariantInfo) + "<br /><br />"
25 + "F: " + DateTime.Parse(thistime).ToString("F", DateTimeFormatInfo.InvariantInfo) + " "
26 + "f: " + DateTime.Parse(thistime).ToString("f", DateTimeFormatInfo.InvariantInfo) + "<br /><br />"
27 + "G: " + DateTime.Parse(thistime).ToString("G", DateTimeFormatInfo.InvariantInfo) + " "
28 + "g: " + DateTime.Parse(thistime).ToString("g", DateTimeFormatInfo.InvariantInfo) + "<br /><br />"
29 + "M: " + DateTime.Parse(thistime).ToString("M", DateTimeFormatInfo.InvariantInfo) + " "
30 + "m: " + DateTime.Parse(thistime).ToString("m", DateTimeFormatInfo.InvariantInfo) + "<br /><br />"
31 + "Y: " + DateTime.Parse(thistime).ToString("Y", DateTimeFormatInfo.InvariantInfo) + " "
32 + "y: " + DateTime.Parse(thistime).ToString("y", DateTimeFormatInfo.InvariantInfo) + "<br /><br />"
33 + "R: " + DateTime.Parse(thistime).ToString("R", DateTimeFormatInfo.InvariantInfo) + " "
34 + "r: " + DateTime.Parse(thistime).ToString("r", DateTimeFormatInfo.InvariantInfo) + "<br /><br />"
35 + "T: " + DateTime.Parse(thistime).ToString("T", DateTimeFormatInfo.InvariantInfo) + " "
36 + "t: " + DateTime.Parse(thistime).ToString("t", DateTimeFormatInfo.InvariantInfo) + "<br /><br />"
37 + "U: " + DateTime.Parse(thistime).ToString("U", DateTimeFormatInfo.InvariantInfo) + " "
38 + "u: " + DateTime.Parse(thistime).ToString("u", DateTimeFormatInfo.InvariantInfo) + "<br /><br />"
39 + "s: " + DateTime.Parse(thistime).ToString("s", DateTimeFormatInfo.InvariantInfo);
40 }
41}
42
效果:
当前时间:
2008-12-1 11:19:10
转换后时间:
D: Monday, 01 December 2008 d: 12/01/2008
F: Monday, 01 December 2008 11:19:10 f: Monday, 01 December 2008 11:19
G: 12/01/2008 11:19:10 g: 12/01/2008 11:19
M: December 01 m: December 01
Y: 2008 December y: 2008 December
R: Mon, 01 Dec 2008 11:19:10 GMT r: Mon, 01 Dec 2008 11:19:10 GMT
T: 11:19:10 t: 11:19
U: Monday, 01 December 2008 03:19:10 u: 2008-12-01 11:19:10Z
s: 2008-12-01T11:19:10