VB.NET ASP.NET String.Format DateTime Source Code Example
| .NET | .NET Programming (C# or VB) (Web or Desktop) | |||
| Key Supporting Languages | HTML | CSS | JavaScript | SQL Server |
| AJAX | ASP | Blogging | ColdFusion | mySQL |
| Perl | PHP | Ruby | Web Design | Web Dev |
String.Format DateTime
Purpose: – Illustrates using String Format DateTime in VB.NET ASP.NET.
Prerequistes:
- Install Visual Web Developer 2008
- Install SQL Server Express
- Download Northwind and Pubs Databases
- Attach Northwind Database to Databases in Sql Express
- Attach pubs Database to Databases in Sql Express
Notes:
- You can build your own library of syntax examples by using same web site over and over and just add new web forms to it.
Instructions:
- Use Visual Web Developer 2008
- Create new web site;
- Click File/New Web Site
- Select ASP.NET Website Template
- Select Visual Basic for Language
- name of Web Site could be VBNET_ASPNET_Syntax.
- Add New folder named "LanguageBasics"
- Right-click project name in solution explorer;
- add new folder;
- name of folder could be: LanguageBasics
- Add Web Form Named StringFormatDateTime to LanguageBasics folder
- Right-click LanguageBasics folder;
- add new item;
- Select Web Form
- Check place code behind in separate file
- Web Form name could be StringFormatDateTime
- Click on copy code in code below to copy code into web form StringFormatDateTime.aspx
- Click on copy code in second set of code below to copy code into code-behind StringFormatDateTime.aspx.vb
- Right-click on StringFormatDateTime.aspx in solution explorer and select view in browser
Step 1: Click on Copy Code to Cut-n-paste code into StringFormatDateTime.aspx
| XML | | copy code | | ? |
| 01 | |
| 02 | <%@ Page Language="VB" AutoEventWireup="false" CodeFile="StringFormatDateTime.aspx.vb" Inherits="LanguageBasics_StringFormatDateTime" %> |
| 03 | |
| 04 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 05 | |
| 06 | <html xmlns="http://www.w3.org/1999/xhtml"> |
| 07 | <head runat="server"> |
| 08 | <title></title> |
| 09 | </head> |
| 10 | <body> |
| 11 | <form id="form1" runat="server"> |
| 12 | <div> |
| 13 | <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> |
| 14 | </div> |
| 15 | </form> |
| 16 | </body> |
| 17 | </html> |
| 18 | |
| 19 | |
| 20 |
Step 2: Click on Copy Code to Cut-n-paste code into StringFormatDateTime.aspx.vb
| Visual Basic | | copy code | | ? |
| 001 | |
| 002 | |
| 003 | Partial Class LanguageBasics_StringFormatDateTime |
| 004 | Inherits System.Web.UI.Page |
| 005 | |
| 006 | |
| 007 | |
| 008 | Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load |
| 009 | |
| 010 | '************************************************** |
| 011 | ' Custom DateTime Formatting |
| 012 | '************************************************** |
| 013 | Dim sb As New StringBuilder |
| 014 | sb.Append("Custom DateTime Formatting:<br />") |
| 015 | |
| 016 | 'Here are custom format specifiers y (year), M (month), d (day), h (hour 12), H (hour 24), m (minute), s (second), |
| 017 | ' f (second fraction), F (second fraction, trailing zeroes are trimmed), t (P.M or A.M) and z (time zone). |
| 018 | |
| 019 | 'Following examples show how the format specifiers can be used. |
| 020 | ' create date time 2009-12-21 20:04:08.101 |
| 021 | Dim dt As New DateTime(2009, 12, 20, 20, 4, 8, _ |
| 022 | 101) |
| 023 | |
| 024 | ' Ways to format year: "9 09 009 2009" year |
| 025 | sb.Append(String.Format("{0:y yy yyy yyyy}", dt) + "<br />") |
| 026 | |
| 027 | ' Ways to format month: "12 12 Dec December" month |
| 028 | sb.Append(String.Format("{0:M MM MMM MMMM}", dt) + "<br />") |
| 029 | |
| 030 | ' Ways to format day: "21 21 Sun Sunday" day |
| 031 | sb.Append(String.Format("{0:d dd ddd dddd}", dt) + "<br />") |
| 032 | |
| 033 | ' Ways to format hour: "8 08 20 20" hour 12/24 |
| 034 | sb.Append(String.Format("{0:h hh H HH}", dt) + "<br />") |
| 035 | |
| 036 | ' Ways to format minute: "4 04" minute |
| 037 | sb.Append(String.Format("{0:m mm}", dt) + "<br />") |
| 038 | |
| 039 | ' Ways to format second: ' "8 08" second |
| 040 | sb.Append(String.Format("{0:s ss}", dt) + "<br />") |
| 041 | |
| 042 | ' Ways to format fraction of second: "1 10 101 1010" sec.fraction |
| 043 | sb.Append(String.Format("{0:f ff fff ffff}", dt) + "<br />") |
| 044 | |
| 045 | ' Ways to format fraction of second: "1 1 101 101" without zeros |
| 046 | sb.Append(String.Format("{0:F FF FFF FFFF}", dt) + "<br />") |
| 047 | |
| 048 | ' How to display am or pm: "P PM" A.M. or P.M. |
| 049 | sb.Append(String.Format("{0:t tt}", dt) + "<br />") |
| 050 | |
| 051 | ' How to display timezone: "-6 -06 -06:00" time zone |
| 052 | sb.Append(String.Format("{0:z zz zzz}", dt) + "<br />") |
| 053 | |
| 054 | |
| 055 | '************************************************** |
| 056 | ' Using date separator / (slash) and time sepatator : (colon). |
| 057 | '************************************************** |
| 058 | sb.Append("<br />") |
| 059 | sb.Append("Using date separator / (slash) and time sepatator : (colon):<br />") |
| 060 | |
| 061 | 'These characters will be rewritten to characters defined |
| 062 | 'in the current DateTimeFormatInfo.DateSeparator and DateTimeFormatInfo.TimeSeparator. |
| 063 | |
| 064 | |
| 065 | ' "20/12/09 20:04:08" - english (en-US) |
| 066 | sb.Append(String.Format("{0:d/M/yyyy HH:mm:ss}", dt) + "<br />") |
| 067 | |
| 068 | ' "20.12.2009 20:04:08" - german (de-DE) |
| 069 | ' date separator in german culture is "." (so "/" changes to ".") |
| 070 | sb.Append(String.Format("{0:d/M/yyyy HH:mm:ss}", dt) + "<br />") |
| 071 | |
| 072 | '************************************************** |
| 073 | ' Here are some examples of custom date and time formatting: |
| 074 | '************************************************** |
| 075 | sb.Append("<br />") |
| 076 | sb.Append("Here are some examples of custom date and time formatting:<br />") |
| 077 | |
| 078 | ' month/day numbers without/with leading zeroes |
| 079 | ' "12/20/2009" |
| 080 | sb.Append(String.Format("{0:M/d/yyyy}", dt) + "<br />") |
| 081 | |
| 082 | ' "12/20/2009" |
| 083 | sb.Append(String.Format("{0:MM/dd/yyyy}", dt) + "<br />") |
| 084 | |
| 085 | ' day/month names |
| 086 | ' "Sun, Dec 20, 2009" |
| 087 | sb.Append(String.Format("{0:ddd, MMM d, yyyy}", dt) + "<br />") |
| 088 | |
| 089 | ' "Sunday, December 20, 2009" |
| 090 | ' two/four digit year |
| 091 | sb.Append(String.Format("{0:dddd, MMMM d, yyyy}", dt) + "<br />") |
| 092 | |
| 093 | ' "12/20/09" |
| 094 | sb.Append(String.Format("{0:MM/dd/yy}", dt) + "<br />") |
| 095 | |
| 096 | ' "12/20/2009" |
| 097 | sb.Append(String.Format("{0:MM/dd/yyyy}", dt) + "<br />") |
| 098 | |
| 099 | '************************************************** |
| 100 | ' Standard DateTime Formatting |
| 101 | '************************************************** |
| 102 | sb.Append("<br />") |
| 103 | sb.Append("Standard DateTime Formatting:<br />") |
| 104 | 'In DateTimeFormatInfo there are defined standard patterns for the current culture. |
| 105 | 'For example, property ShortTimePattern is string that contains value h:mm tt for en-US culture |
| 106 | 'and value HH:mm for de-DE culture. |
| 107 | |
| 108 | 'Following table shows patterns defined in DateTimeFormatInfo and their values for en-US culture. |
| 109 | 'First column contains format specifiers for the String.Format method. |
| 110 | |
| 111 | 'Specifier DateTimeFormatInfo property Pattern value (for en-US culture) |
| 112 | 't ShortTimePattern h:mm tt |
| 113 | 'd ShortDatePattern M/d/yyyy |
| 114 | 'T LongTimePattern h:mm:ss tt |
| 115 | 'D LongDatePattern dddd, MMMM dd, yyyy |
| 116 | 'f (combination of D and t) dddd, MMMM dd, yyyy h:mm tt |
| 117 | 'F FullDateTimePattern dddd, MMMM dd, yyyy h:mm:ss tt |
| 118 | 'g (combination of d and t) M/d/yyyy h:mm tt |
| 119 | 'G (combination of d and T) M/d/yyyy h:mm:ss tt |
| 120 | 'm, M MonthDayPattern MMMM dd |
| 121 | 'y, Y YearMonthPattern MMMM, yyyy |
| 122 | 'r, R RFC1123Pattern ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (*) |
| 123 | 's SortableDateTimePattern yyyy'-'MM'-'dd'T'HH':'mm':'ss (*) |
| 124 | 'u UniversalSortableDateTimePattern yyyy'-'MM'-'dd HH':'mm':'ss'Z' (*) |
| 125 | ' (*) = culture independent |
| 126 | |
| 127 | 'Following examples show usage of standard format specifiers in String.Format method and the resulting output. |
| 128 | |
| 129 | ' "8:04 PM" ShortTime |
| 130 | sb.Append(String.Format("{0:t}", dt) + "<br />") |
| 131 | |
| 132 | ' "12/20/2009" ShortDate |
| 133 | sb.Append(String.Format("{0:d}", dt) + "<br />") |
| 134 | |
| 135 | ' "8:04:08 PM" LongTime |
| 136 | sb.Append(String.Format("{0:T}", dt) + "<br />") |
| 137 | |
| 138 | ' "Sunday, December 20, 2009" LongDate |
| 139 | sb.Append(String.Format("{0:D}", dt) + "<br />") |
| 140 | |
| 141 | ' "Sunday, December 20, 2009 8:04 PM" LongDate+ShortTime |
| 142 | sb.Append(String.Format("{0:f}", dt) + "<br />") |
| 143 | |
| 144 | ' "Sunday, December 20, 2009 8:04:08 PM" FullDateTime |
| 145 | sb.Append(String.Format("{0:F}", dt) + "<br />") |
| 146 | |
| 147 | ' "12/20/2009 8:04 PM" ShortDate+ShortTime |
| 148 | sb.Append(String.Format("{0:g}", dt) + "<br />") |
| 149 | |
| 150 | ' "12/20/2009 8:04:08 PM" ShortDate+LongTime |
| 151 | sb.Append(String.Format("{0:G}", dt) + "<br />") |
| 152 | |
| 153 | ' "December 20" MonthDay |
| 154 | sb.Append(String.Format("{0:m}", dt) + "<br />") |
| 155 | |
| 156 | ' "December, 2009" YearMonth |
| 157 | sb.Append(String.Format("{0:y}", dt) + "<br />") |
| 158 | |
| 159 | ' "Sun, 20 Dec 2009 20:04:08 GMT" |
| 160 | sb.Append(String.Format("{0:r}", dt) + "<br />") |
| 161 | |
| 162 | ' "2009-12-20T20:04:08" SortableDateTime |
| 163 | sb.Append(String.Format("{0:s}", dt) + "<br />") |
| 164 | |
| 165 | ' "2009-12-20 20:04:08Z" UniversalSortableDateTime |
| 166 | sb.Append(String.Format("{0:u}", dt) + "<br />") |
| 167 | |
| 168 | Label1.Text = sb.ToString |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | End Sub |
| 175 | End Class |
| 176 | |
| 177 | |
| 178 |
Step 3: Click on Copy Code to Cut-n-paste code into web.config right after the appSettings section
| XML | | copy code | | ? |
| 1 | |
| 2 | <connectionStrings> |
| 3 | <add name="Northwind_ConnectionString" |
| 4 | connectionString="Server=(local)\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI" /> |
| 5 | <add name="Pubs_ConnectionString" |
| 6 | connectionString="Server=(local)\SQLEXPRESS;Initial Catalog=pubs;Integrated Security=SSPI" /> |
| 7 | </connectionStrings> |
| 8 |
Related posts:
- C# ASP.NET String.Format DateTime Source Code Example C# ASP.NET String.Format DateTime Source Code Example...
- VB.NET String Format DateTime Source Code Example VB.NET String Format DateTime Source Code Example...
- C# String.Format DateTime Source Code Example C# String.Format DateTime Source Code Example...
- VB.NET ASP.NET StringBuilder Source Code Example VB.NET ASP.NET StringBuilder Source Code Example...
- VB.NET StringBuilder Source Code Example VB.NET StringBuilder Source Code Example...
Related posts brought to you by Yet Another Related Posts Plugin.
