ASP.Net VB Format DateTime Using ToString Function

by top54u.com 23 Jul, 2008

In ASP.Net 2.0, VB Format DateTime using ToString function can used to format the Date, Time and DateTime each. In the previous tutorial we learnt the ASP.Net VB Format Date using ToString Function. Here we will learn the use of same ToString function to format the Time parts as well as DateTime combinations. As discussed earlier Date parts can be formatted using alphabetical letters such as M for month, d for day and y for year. Similarly Time parts are also represented using alphabetical letters such as h or H for hours, m for minutes, s for seconds and t for meridians [AM / PM]. Note that uppercase letter M represents month as lowercase letter m denotes minutes.

You can use the following DateTime Formats in ASP.Net VB code:

 

  • hh:mm:ss tt

  • hh:mm ss

  • hh:mm tt

  • HH:mm ss tt

  • HH:mm ss

  • HH:mm tt

  • MM/dd/yyyy hh:mm:ss tt

  • MM/dd/yyyy HH:mm:ss tt

 

 

You can customize these DateTime and Time formats using different patterns.

Lowercase h shows time in 12 hour format.

Uppercase H shows time in 24 hour format.

 

Examples of ASP.Net VB Format DateTime using ToString Function

 

' Displays time format hh:mm:ss tt

' h shows 12 hour time format

' 12:00:00 AM = 12:00:00 AM

Response.Write(DateTime.Now.ToString("hh:mm:ss tt"))

Response.Write("<br>")

 

' Displays time format hh:mm ss

Response.Write(DateTime.Now.ToString("hh:mm ss"))

Response.Write("<br>")

 

' Displays time format hh:mm tt

Response.Write(DateTime.Now.ToString("hh:mm tt"))

Response.Write("<br>")

 

' Displays time format HH:mm:ss tt

' H shows 24 hour time format

' 12:00:00 AM = 00:00:00 AM

Response.Write(DateTime.Now.ToString("HH:mm:ss tt"))

Response.Write("<br>")

 

' Displays time format HH:mm ss

Response.Write(DateTime.Now.ToString("HH:mm ss"))

Response.Write("<br>")

 

' Displays time format HH:mm tt

Response.Write(DateTime.Now.ToString("HH:mm tt"))

Response.Write("<br>")

 

' Displays DateTime format MM/dd/yyyy hh:mm:ss tt

' Time in 12 hour format

Response.Write(DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt"))

Response.Write("<br>")

 

' Displays DateTime format MM/dd/yyyy HH:mm:ss tt

' Time in 24 Hour format

Response.Write(DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss tt"))

 

Output:

05:54:04 PM
05:54 04
05:54 PM
17:54:04 PM
17:54 04
17:54 PM
07/22/2008 05:54:04 PM
07/22/2008 17:54:04 PM

Spotlight.....

Currently rated 4.7 by 3 people

  • Currently 4.666667/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , , , , , , , , ,

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

10/11/2008 3:32:23 AM




related videos.....
recent posts.....
top54u ezines.....