日期&时间分离

时间:2011-05-25 13:50:31

标签: c# asp.net timezone

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="inpHide" type="hidden" runat="server" />
        <input id="inpHide1" type="hidden" runat="server" />
        <asp:Label ID="Label1" runat="server"></asp:Label>
        <asp:Label ID="Label2" runat="server"></asp:Label>
        <asp:Label ID="Label3" runat="server"></asp:Label>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Width="100px" Height="30px" Text="Button" />
    </div>
    <script language="javascript" type="text/javascript">
        var current_date = new Date();
        var current_timezone = current_date.getTimezoneOffset();
        document.getElementById("inpHide").value = current_timezone * -1;
        document.getElementById("inpHide1").value = current_date;
        </script>
</body>
</html>

protected void Button1_Click(object sender, EventArgs e)
{
        Label1.Text = inpHide.Value.Trim();
            Label2.Text = inpHide1.Value.Trim();
            Label3.Text = Label2.Text;
}

4 个答案:

答案 0 :(得分:5)

DateTime dt;
string Temp1 = "Your Date";
if (Temp1.LastIndexOf("GMT") > 0)
{
    Temp1 = Temp1.Remove(Temp1.LastIndexOf("GMT"));
}
Temp1 = "Wed May 25 23:43:31 UTC+0900 2011";
if (Temp1.LastIndexOf("UTC") > 0)
{
     Temp1 = Temp1.Remove(Temp1.LastIndexOf("UTC"), 9);
     string[] split = Temp1.Split(' ');
     Temp1 = split[0] + " " + split[1] + " " + split[2] + " " + split[4] + " " + split[3];
}
if (DateTime.TryParse(Temp1, out dt))
{
     // If it is a valid date
     string date = dt.ToShortDateString();
     string time = dt.ToShortTimeString();
}

答案 1 :(得分:4)

我使用字符串格式,如www.csharp-examples.net的C# Examples页面所示:

String.Format("{0:t}", dt);  // "4:05 PM"                         ShortTime
String.Format("{0:d}", dt);  // "3/9/2008"                        ShortDate

答案 2 :(得分:2)

您应该查看msdn documentation around DateTime

您应该特别注意解析函数和各种ToString函数,注意传入的字符串格式(或根据当前文化),以确保您获得您期望的格式。

答案 3 :(得分:0)

我不确定您使用的是哪种编程语言,但大多数都具有在空白处打破字符串的功能。你可以用这种方式打破字符串,然后取出日期元素并使用它们。