开发者

How to display full server date and time in VB.NET?

开发者 https://www.devze.com 2023-01-26 05:39 出处:网络
How to displ开发者_JAVA百科ay full server date and time in vb.net ? i want to display server date as 20-Nov-2010 in textbox1 and time as 08:11:00 AM in Textbox2 on page load eventIf you want to displ

How to displ开发者_JAVA百科ay full server date and time in vb.net ?

i want to display server date as 20-Nov-2010 in textbox1 and time as 08:11:00 AM in Textbox2 on page load event


If you want to display server time:

Markup:

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

Code-behind:

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 

If you want to display client time:

Markup:

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


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


Like this:

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

Update: (following updated question)

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

You may want to read up on custom DateTime format strings.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号