如何在VB.NET中显示完整的服务器日期和时间?

时间:2010-11-20 10:19:22

标签: asp.net vb.net

如何在vb.net中显示完整的服务器日期和时间?

我想在textbox1中将服务器日期显示为20-Nov-2010,在页面加载事件的Textbox2中将时间显示为08:11:00 AM

3 个答案:

答案 0 :(得分:2)

如果要显示服务器时间:

标记:

<asp:TextBox runat="server" ID="TextBox1" />
<br />
<asp:TextBox runat="server" ID="TextBox2" />

代码隐藏:

Imports System.Globalization

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    ' pay attention to DateTime.Today '
    TextBox1.Text = DateTime.Today.ToString("dd-MMM-yyyy")

    ' culture-specific: '
    Dim ci As CultureInfo = new CultureInfo("en-US")
    TextBox2.Text = DateTime.Now.ToString("T", ci)

    ' or culture-independent: '
    TextBox2.Text = DateTime.Now.ToString("hh:mm:ss tt")
End Sub 

如果您想显示客户时间:

标记:

<!-- Adding the reference to jQuery CDN, for example, by Google -->
<script type="text/javascript" language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

<!-- Formatting function -->
<script type="text/javascript" language="javascript">
    $(document).ready(function () {
        var names = ["Jan", "Feb", "March", "Apr", "May", "June", "July", "Aug", "Sepr", "Oct", "Nov", "Dec"];
        var now = new Date();
        $('#TextBox1').val(now.getDate() + '-' + names[now.getMonth()] + '-' + now.getFullYear());
        var minutes = now.getMinutes();
        $('#TextBox2').val(now.getHours() + ':' + formatLeadingZero(minutes) + ':' + formatLeadingZero(now.getSeconds()) + ' ' + formatTimePeriod(minutes));
    });

    function formatLeadingZero(value) {
        return (value < 10) ? '0' + value : value;
    }

    function formatTimePeriod(value) {
        return (value < 12) ? "AM" : "PM";
    }
</script>

<!-- ASP.NET controls -->
<asp:TextBox runat="server" ID="TextBox1" />
<br />
<asp:TextBox runat="server" ID="TextBox2" />

答案 1 :(得分:1)

像这样:

Response.Write(DateTime.Now.ToString())

更新:(更新后的问题)

Dim now As DateTime = DateTime.Now
textbox1.Text = now.ToString("dd-MMM-yyyy")
textbox2.Text = now.ToString("hh:mm:ss tt")

您可能需要阅读custom DateTime format strings

答案 2 :(得分:1)

textbox1.Text = DateTime.Now.ToString("dd-MMM-yyyy")
textbox2.Text = DateTime.Now.ToString("hh:mm:ss tt")