Convert Date/Time values into strings in VB.NET
Takeaway: Learn how to convert date and time values into formatted strings by overloading a version of the ToString method, which accepts a format string.
Working with date and time values often requires an ability to convert these values into string values in a particular format. In this article, I'll look at a simple way of converting these values into formatted strings.
Working with standard format strings
You can convert a Date/Time value into a string by overloading a version of the ToString method, which accepts a format string.
There are two types of Date/Time format strings: standard and custom. The standard format strings use one of the predefined formats and are specified by a letter corresponding to a particular format. When converting a Date/Time value to a string, the letter specifying one of the standard formats is used to refer to a particular format specifier. Here's a list of the standard date format specifiers:
- d: short date
- D: long date
- t: short time
- T: long time
- f: full Date/Time with short time
- F: full Date/Time with long time
- g: general Date/Time with short time
- G: general Date/Time with long time
- M or m: month day
- R or r: RFC1123
- s: sortable Date/Time that conforms to ISO 8601
- u: universal sortable Date/Time
- U: full Date/Time with long time. (This format is the same as F, but it uses universal GMT time.)
- Y or y: year month
Control Panel settings
The settings in the Control Panel's Regional and Language Options section influence the result you get when formatting a Date/Time value. The settings are used to initialize the DateTimeFormatInfo object, which is associated with the current thread culture and provides the values that control the formatting.
Dim dateTimeInfo as DateTime = DateTime.Now
MessageBox.Show (dateTimeInfo)
Dim strMonth as String = dateTimeInfo.ToString("m")
MessageBox.Show(strMonth)
The code defines the DateTime variable dateTimeInfo and sets its value to the current Date/Time. Then, I define the String variable strMonth and convert the value of dateTimeInfo into a String in a Month format.
Custom Date/Time format specifiers
You can also create custom format strings when the available standard format strings don't satisfy your needs. A custom Date/Time format string consists of one or more custom Date/Time format specifiers and defines how the Date/Time data would be formatted. For a full list of custom format specifiers, visit MSDN.
Miss a tip?
Check out the Visual Basic archive, and catch up on the most recent editions of Irina Medvinskaya's column.
Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!
White Papers, Webcasts, and Downloads
- Software Trial: AdminStudio(r) Migrates MSIs to Windows(r) 7 and App-V(r) Fast Flexera Software AdminStudio? allows IT to quickly prepare reliable virtual and MSI ... Download Now
- Business Analytics and Optimization for the Intelligent Enterprise IBM Corp. IBM Global Business Services, through the IBM Institute for Business ... Download Now
- Top 7 Things You Should Know About Activation and Genuine Windows Microsoft As an IT Pro, you should be aware that volume activation is a required ... Download Now
- Creating a Dynamic Information Infrastructure IBM Corp. IBM Information Infrastructure solutions can help reduce costs & transform ... Download Now
- Responding to Today's Demands with a Dynamic Infrastructure IBM Corp. Listen to this webcast to hear IBM executives and clients discuss a host ... Download Now
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET
