Create a date/time value from date parts in VB.NET
Takeaway: Irina Medvinskaya shows you how to put together a full date/time value from a combination of its specific parts. This VB.NET tip also includes sample code.
When you need to put together a full date/time value from a combination of its specific parts, you can overload the Date object's constructors and assign the necessary date/time value parts to it. This tip shows how to perform the functionality.
Putting together a date/time value
There is more than one way to put together a date/time value in VB.NET. Listing A shows different ways of achieving the same functionality.
- The first message box will show: dtDate1: 1/2/2007 12:00:00 A.M.
- The second message box will show: dtDate2: 1/2/2007 12:00:00 A.M.
- The third message box will show: dtDate3: 2/3/2007 3:01:59 A.M.
- The fourth message box will show: dtDate4: 2/3/2007 3:01:59 P.M.
Notes about the example
To set the value of the variable dtDate1, I utilize the # sign to set the date to 1/2/2007. Since the time is not specified, the system will assign the time 12:00:00 A.M. by default. (If you had specified #1/2/2007 3:59:00 PM#, the time assigned would have been 3:59:00 P.M.)
To set the value of the variable dtDate2, I utilize the Date object, passing to it the same date values as for dtDate1. The result is the same; dtDate2 shows exactly the same date as dtDate1 and, since I skipped the time, the time is also set by default to 12:00:00 A.M.
To set the value of the variable dtDate3, I provide full information including the time. Notice that I use military time to set it to 3:00:00 A.M.
To set the value of the variable dtDate4, I use the same date and time as in dtDate3, but I use 15:00:00 for the hour, which results in 3:00:00 P.M.
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
- VMware Infrastructure: A Guide to Bottom-Line Benefits VMware Frustrated by the high cost of maintaining or building ever-larger data centers? Get the facts you need to formulate your Virtualization Action Plan. Download Now
- Tom Davenport Study: Linking decisions and information for organizational performance IBM Tom Davenport's new client study looks at approaches to linking ... Download Now
- Building the Virtualized Enterprise with VMware Iinfrastructure VMware VMware virtualization software has been adopted by over 120,000 enterprise ... Download Now
- Five Steps to Determine When to Virtualize YourServers VMware Thinking of virtualizing the servers at your company? Use this step-by-step guide to determine when's the best time to make your big move. Download Now
- Why Isn't Server Virtualization Saving Us More? A Few Small Changes May Dramatically Increase Your Efficiency VMware Ever wonder why your company isn't saving more from its server virtualization? Making a few small changes could dramatically increase your efficiency. 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
Designing the next killer product
Developing new ways to collaborate
Overseeing IT operations across a global organization
The biggest security threats facing companies in 2009
