VB.NET String Format DateTime Source Code Example

"I tried to learn .NET by taking boot camp classes that cost me $6,000. After taking the classes, I discovered tons of free (and very cheap) videos on the internet created by experts in the field. I found the videos to be much better than the classrooms. The videos were presented by Microsoft presenters that knew the material backwards and forwards, and they were much cheaper and better than the classroom teachers. I have spent the last three years organizing and hunting down the videos for my own use and decided that all could benefit by sharing what I found." - Wade Harvey

Click Here for Free 24 hour pass to lynda.com.

I AM VERY EXCITED ABOUT THIS AMAZING NEW DISCOVERY

Over 40,000 Top-Notch Video Tutorials (or 3,452 hours) on Lynda.com

I had stumbled across Lynda.com many times before, but never stopped to try it out. I recently took the plunge and started using Lynda.com to help me improve my javascript skills (See Javascript Essential Training 2007 by Dori Smith). It has revolutionized my understanding of the language! I love the fact that the exercise files allow you to follow along in real time with what the instructor is saying. Learning by doing is an extremely powerful technique. I have to give the site an A+ for both quality and content.

I am planning on using Lynda Videos to help me improve in:

  1. HTML
  2. CSS
  3. Classic ASP
  4. .NET (included in ASP section)
  5. AJAX
  6. Silverlight (in Microsoft Section)
  7. SharePoint (in Microsoft Section)
  8. Web Design
  9. Blogging
  10. SEO
  11. and much more
You can learn more about Lynda.com by watching the 5-minute video at About Lynda.com. If you are only focusing on .NET, you are missing out on the "big picture" and on many of the underlying fundamentals! To be an Ideal Programmer, you need to have the largest knowledge base possible.

JavaScript tutorials



Looking for premium .NET Training Videos? The best premium .NET Videos that I have found are at Learn Visual Studio. Those videos are only about 50 cents per hour, as compared to $25 per hour that other sites charge!

LearnVisualStudio is currently having a 25% off sale ($50 savings) and giving away a free membership to TrainingSpot ($60 value) when LVS Lifetime Membership is purchased.

  

DiscountASP .NET hosting  JavaScript tutorials


Limited Time Offer: Free Lifetime Membership to Progress Monitor ($24.95 value) when you purchase any membership at Learn Visual Studio, Lynda Videos or any hosting account at DiscountASP - just forward order confirmation to harvey007@sbcglobal.net

String Format DateTime

Purpose: – Illustrates using .

Prerequistes:

  1. Install Visual Basic (Express or Standard Edition)
  2. Install SQL Server Express
  3. Download Northwind and pubs Database
  4. Attach Northwind Database to Databases in Sql Express
  5. Attach pubs Database to Databases in Sql Express

Notes:

  • Console Application is used to simplify things, but Windows Forms or Web Forms could also be used
  • You can build a library of syntax examples by using same project over and over and just commenting out what you do not want to execute in Module1.vb

Instructions:

  1. Use Visual Basic 2008 Express or Standard Edition
  2. Create new project;
    • Click File/New Project
    • Select Console Application Template
    • Select Visual Basic for Language
    • name of project could be VBNET_Syntax.
  3. Add New folder named "Language-Basics"
    • Right-click project name in solution explorer;
    • add new folder;
    • name of folder could be: Language-Basics
  4. Add Class Named clsStringFormatDateTime to Language-Basics folder
    • Right-click Language-Basics folder;
    • add new item;
    • Select class
    • Class name could be clsStringFormatDateTime
  5. Click on copy code in code below to copy code into clsStringFormatDateTime.vb
  6. Click on copy code in second set of code below to copy code into Module1.vb
  7. Click green arrow or press F5 to run program

Step 1: Use Copy Code to Cut-n-paste code into clsStringFormatDateTime.vb

 Visual Basic |  copy code |? 
001
002
Public Class clsStringFormatDateTime
003
 
004
    Public Sub Main()
005
        '**************************************************
006
        '        Custom DateTime Formatting
007
        '**************************************************
008
 
009
        Console.WriteLine("Custom DateTime Formatting:")
010
        'Here are custom format specifiers y (year), M (month), d (day), h (hour 12), H (hour 24), m (minute), s (second),
011
        ' f (second fraction), F (second fraction, trailing zeroes are trimmed), t (P.M or A.M) and z (time zone).
012
 
013
        'Following examples show how the format specifiers can be used.
014
        ' create date time 2009-12-21 20:04:08.101
015
        Dim dt As New DateTime(2009, 12, 20, 20, 4, 8, _
016
         101)
017
 
018
        ' Ways to format year: "9 09 009 2009"   year
019
        Console.WriteLine(String.Format("{0:y yy yyy yyyy}", dt))
020
 
021
        ' Ways to format month: "12 12 Dec December"  month
022
        Console.WriteLine(String.Format("{0:M MM MMM MMMM}", dt))
023
 
024
        ' Ways to format day: "21 21 Sun Sunday" day
025
        Console.WriteLine(String.Format("{0:d dd ddd dddd}", dt))
026
 
027
        ' Ways to format hour: "8 08 20 20"      hour 12/24
028
        Console.WriteLine(String.Format("{0:h hh H HH}", dt))
029
 
030
        ' Ways to format minute: "4 04"            minute
031
        Console.WriteLine(String.Format("{0:m mm}", dt))
032
 
033
        ' Ways to format second:  ' "8 08"            second 
034
        Console.WriteLine(String.Format("{0:s ss}", dt))
035
 
036
        ' Ways to format fraction of second: "1 10 101 1010"   sec.fraction
037
        Console.WriteLine(String.Format("{0:f ff fff ffff}", dt))
038
 
039
        ' Ways to format fraction of second: "1 1 101 101"   without zeros
040
        Console.WriteLine(String.Format("{0:F FF FFF FFFF}", dt))
041
 
042
        ' How to display am or pm: "P PM"            A.M. or P.M.
043
        Console.WriteLine(String.Format("{0:t tt}", dt))
044
 
045
        ' How to display timezone: "-6 -06 -06:00"   time zone
046
        Console.WriteLine(String.Format("{0:z zz zzz}", dt))
047
 
048
 
049
        '**************************************************
050
        '        Using date separator / (slash) and time sepatator : (colon).
051
        '**************************************************
052
        Console.WriteLine(vbCrLf)
053
        Console.WriteLine("Using date separator / (slash) and time sepatator : (colon):")
054
 
055
        'These characters will be rewritten to characters defined 
056
        'in the current DateTimeFormatInfo.DateSepa­rator and DateTimeFormatInfo.TimeSeparator.
057
 
058
 
059
        ' "20/12/09 20:04:08" - english (en-US)
060
        Console.WriteLine(String.Format("{0:d/M/yyyy HH:mm:ss}", dt))
061
 
062
        ' "20.12.2009 20:04:08" - german (de-DE)
063
        ' date separator in german culture is "." (so "/" changes to ".")
064
        Console.WriteLine(String.Format("{0:d/M/yyyy HH:mm:ss}", dt))
065
 
066
        '**************************************************
067
        ' Here are some examples of custom date and time formatting:
068
        '**************************************************
069
        Console.WriteLine(vbCrLf)
070
        Console.WriteLine("Here are some examples of custom date and time formatting:")
071
 
072
        ' month/day numbers without/with leading zeroes
073
        ' "12/20/2009"
074
        Console.WriteLine(String.Format("{0:M/d/yyyy}", dt))
075
 
076
        ' "12/20/2009"
077
        Console.WriteLine(String.Format("{0:MM/dd/yyyy}", dt))
078
 
079
        ' day/month names
080
        ' "Sun, Dec 20, 2009"
081
        Console.WriteLine(String.Format("{0:ddd, MMM d, yyyy}", dt))
082
 
083
        ' "Sunday, December 20, 2009"
084
        ' two/four digit year
085
        Console.WriteLine(String.Format("{0:dddd, MMMM d, yyyy}", dt))
086
 
087
        ' "12/20/09"
088
        Console.WriteLine(String.Format("{0:MM/dd/yy}", dt))
089
 
090
        ' "12/20/2009"
091
        Console.WriteLine(String.Format("{0:MM/dd/yyyy}", dt))
092
 
093
        '**************************************************
094
        '        Standard DateTime Formatting
095
        '**************************************************
096
        Console.WriteLine(vbCrLf)
097
        Console.WriteLine("Standard DateTime Formatting:")
098
        'In DateTimeFormatInfo there are defined standard patterns for the current culture. 
099
        'For example, property ShortTimePattern is string that contains value h:mm tt for en-US culture 
100
        'and value HH:mm for de-DE culture.
101
 
102
        'Following table shows patterns defined in DateTimeFormatInfo and their values for en-US culture. 
103
        'First column contains format specifiers for the String.Format method.
104
 
105
        'Specifier DateTimeFormatInfo property Pattern value (for en-US culture) 
106
        't ShortTimePattern h:mm tt 
107
        'd ShortDatePattern M/d/yyyy 
108
        'T LongTimePattern h:mm:ss tt 
109
        'D LongDatePattern dddd, MMMM dd, yyyy 
110
        'f (combination of D and t) dddd, MMMM dd, yyyy h:mm tt 
111
        'F FullDateTimePattern dddd, MMMM dd, yyyy h:mm:ss tt 
112
        'g (combination of d and t) M/d/yyyy h:mm tt 
113
        'G (combination of d and T) M/d/yyyy h:mm:ss tt 
114
        'm, M MonthDayPattern MMMM dd 
115
        'y, Y YearMonthPattern MMMM, yyyy 
116
        'r, R RFC1123Pattern ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (*) 
117
        's SortableDateTi­mePattern yyyy'-'MM'-'dd'T'HH':'mm':'ss (*) 
118
        'u UniversalSorta­bleDateTimePat­tern yyyy'-'MM'-'dd HH':'mm':'ss'Z' (*) 
119
        '    (*) = culture independent 
120
 
121
        'Following examples show usage of standard format specifiers in String.Format method and the resulting output.
122
 
123
        ' "8:04 PM"                         ShortTime
124
        Console.WriteLine(String.Format("{0:t}", dt))
125
 
126
        ' "12/20/2009"                        ShortDate
127
        Console.WriteLine(String.Format("{0:d}", dt))
128
 
129
        ' "8:04:08 PM"                      LongTime
130
        Console.WriteLine(String.Format("{0:T}", dt))
131
 
132
        ' "Sunday, December 20, 2009"          LongDate
133
        Console.WriteLine(String.Format("{0:D}", dt))
134
 
135
        ' "Sunday, December 20, 2009 8:04 PM"  LongDate+ShortTime
136
        Console.WriteLine(String.Format("{0:f}", dt))
137
 
138
        ' "Sunday, December 20, 2009 8:04:08 PM" FullDateTime
139
        Console.WriteLine(String.Format("{0:F}", dt))
140
 
141
        ' "12/20/2009 8:04 PM"                ShortDate+ShortTime
142
        Console.WriteLine(String.Format("{0:g}", dt))
143
 
144
        ' "12/20/2009 8:04:08 PM"             ShortDate+LongTime
145
        Console.WriteLine(String.Format("{0:G}", dt))
146
 
147
        ' "December 20"                        MonthDay
148
        Console.WriteLine(String.Format("{0:m}", dt))
149
 
150
        ' "December, 2009"                     YearMonth
151
        Console.WriteLine(String.Format("{0:y}", dt))
152
 
153
        ' "Sun, 20 Dec 2009 20:04:08 GMT"   
154
        Console.WriteLine(String.Format("{0:r}", dt))
155
 
156
        ' "2009-12-20T20:04:08"             SortableDateTime
157
        Console.WriteLine(String.Format("{0:s}", dt))
158
 
159
        ' "2009-12-20 20:04:08Z"            UniversalSortableDateTime
160
        Console.WriteLine(String.Format("{0:u}", dt))
161
 
162
 
163
 
164
        Console.ReadLine()
165
    End Sub
166
End Class
167
 
168
 
169
 
170

Step 2: Use View Plain to Cut-n-paste code into Module1.vb
 Visual Basic |  copy code |? 
01
02
Module Module1
03
 
04
    Sub Main()
05
        '***** Language-Basics *************
06
 
07
         Dim myStringFormatDateTime As New clsStringFormatDateTime
08
        myStringFormatDateTime.Main()
09
 
10
 
11
 
12
    End Sub
13
 
14
End Module
15
 
16

Related posts:

  1. C# String.Format DateTime Source Code Example C# String.Format DateTime Source Code Example...
  2. VB.NET ASP.NET String.Format DateTime Source Code Example VB.NET ASP.NET String.Format DateTime Source Code Example...
  3. C# ASP.NET String.Format DateTime Source Code Example C# ASP.NET String.Format DateTime Source Code Example...
  4. VB.NET StringBuilder Source Code Example VB.NET StringBuilder Source Code Example...
  5. VB.NET Sql Command Update Statement Source Code Example VB.NET Source Code Example shows how to use Sql Command...

Related posts brought to you by Yet Another Related Posts Plugin.

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

This blog uses the cross-linker plugin developed by Web-Developers.Net

http://idealprogrammer.com/home/idearcom/public_html/wp-content/plugins/wp-super-cache/