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

标签 asp.net vb.net

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

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

最佳答案

如果你想显示服务器时间:

标记:

<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" />

关于asp.net - 如何在 VB.NET 中显示完整的服务器日期和时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4232234/

相关文章:

c# - 从 VB.Net 迁移到 C#

c# - VB "If(a & b)"中这个语法的含义是什么

c# - 在 LINQ Select 期间访问递增整数

asp.net - 如何让子项出现在 ASP.NET 的自定义 Web 控件中?

c# - imageresizer.net性能基准

c# - 使用jquery只检查gridview中的一个复选框

javascript - 正则表达式 javascript 到 vb

c# - 有没有办法获得来自 XSLT 转换 'on the fly' 的 asp.net 页面?

c# - 使用网络上的主机名获取 ip 地址 asp.net

vb.net - 是否可以在 VB.net 中声明本地/类实例成员引用变量以读取/更新另一个变量引用的同一对象