简体   繁体   中英

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

How to display 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" />

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM