Advertisement

08.04.2007 at 05:03PM PDT, ID: 22742341
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

VB.NET: Convert Date string and Time string to DateTime value
Tags: date, convert, string, time, datetime
I have a web app developed in Visual Studio (vb.net) that ties to a SQL Server database.  One of the fields in the database is a datetime field.  The form has two text boxes, txtDate, and txtTime.  txtDate takes a regular expression along standard m/d/yyyy format (e.g., "1/1/2007", "12/13/2007").  txtTime takes a regular expression along standard time notation (e.g., "5:00 PM", "12:00 PM", "4:21 PM").

I need to convert these two separate strings into a datetime object so I can store it in the database.

I know this sounds simple, but I've been having a ton of problems with dates, times, datetime, and timespans.  It's not quite intuitive.

Any help?
Start your free trial to view this solution
Question Stats
Zone: Microsoft
Question Asked By: kmoloney
Solution Provided By: Arthur_Wood
Participating Experts: 4
Solution Grade: A
Views: 992
Translate:
Loading Advertisement...
08.04.2007 at 05:45PM PDT, ID: 19632490

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2007 at 05:53PM PDT, ID: 19632501

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2007 at 05:58PM PDT, ID: 19632511

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2007 at 06:20PM PDT, ID: 19632560

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2007 at 07:31PM PDT, ID: 19632743

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2007 at 11:08PM PDT, ID: 19633333

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.05.2007 at 12:12AM PDT, ID: 19633446

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.05.2007 at 12:17AM PDT, ID: 19633456

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.05.2007 at 12:24AM PDT, ID: 19633474

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.05.2007 at 12:42AM PDT, ID: 19633503

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.07.2007 at 04:13PM PDT, ID: 19650332

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
08.04.2007 at 05:45PM PDT, ID: 19632490

Rank: Guru

Dim NewDateTime as DateTime =cType(txtDate.Text & " " & txtTime.Text, DateTime)

txtMyDateTime = NewDateTime

The additional " " is important.

AW
Accepted Solution
 
08.04.2007 at 05:53PM PDT, ID: 19632501

Rank: Master

That is correct.  Same thing but a bit more .NET-ish and less VB-ish:

    Dim dt As DateTime = DateTime.Parse(txtDate.Text + " " + txtTime.Text)
 
08.04.2007 at 05:58PM PDT, ID: 19632511

Rank: Guru

6 to one, half dozen to the other.

The two are functionally identical.

You pays your money, and you takes your choice.

Can you say split the points?? LOL   ;-}

AW
 
08.04.2007 at 06:20PM PDT, ID: 19632560

Rank: Genius

Just a minor note...

With VB.Net 2005, I would use one of the DateTime.TryParse() methods, instead of DateTime.Parse(), as it allows for easier code to be written:
http://msdn2.microsoft.com/en-us/library/ch92fbc1.aspx
 
08.04.2007 at 07:31PM PDT, ID: 19632743

Rank: Master

Yes, TryParse is useful if your input format is not validated and you expect failures.  Parse defers to TryParse internally anyway and throws an exception if TryParse returns false.

And no, I don't expect any point splitting - it was just a reaction to seeing a lot of questions about converting VB to C#.  It's an easier process if you stick to the framework ways of doing things rather than using VB-specific syntax.  As AW points out, the net result is the same.  In fact, the CType statement ultimately calls DateTie.TryParse as well.  Everything ends up in the same happy place.
 
08.04.2007 at 11:08PM PDT, ID: 19633333
will this work with the regular expressions for a "DATE" and a "TIME":

DATE regex:

(0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?\d\d

TIME regex:

([0-2])?\d:\d\d [AP]M

I guess I'll try it and find out.

BTW, its up to me if I want to split points... :-)
 
08.05.2007 at 12:12AM PDT, ID: 19633446
Error received:

Conversion from string "" to type double is not valid.
 
08.05.2007 at 12:17AM PDT, ID: 19633456
Here's my code (if they leave the time field blank, it's assumed to be 5:00 PM)

    Private Sub DetermineDateTimeFormat(ByRef strBody As String)
        '// DETERMINE THE DATE FORMAT TO BE PRESENT IN THE LETTER
        If Me.txtTermTime.Text = "" Then
            t.TerminationDateTime = CType(Me.txtTermDate.Text & " " & "5:00 PM", System.DateTime)
        Else
            t.TerminationDateTime = CType(Me.txtTermDate.Text & " " * Me.txtTermTime.Text, System.DateTime)

        End If
    End Sub
 
08.05.2007 at 12:24AM PDT, ID: 19633474

Rank: Master

You've got a '*' where a '&' shoudl be in the else clause:

t.TerminationDateTime = CType(Me.txtTermDate.Text & " " * <--- here
Me.txtTermTime.Text, System.DateTime)
Assisted Solution
 
08.05.2007 at 12:42AM PDT, ID: 19633503

Rank: Master

Not sure what you're doing with the regex, but they are somewhat oversimplified.  The time ones are not too bad to improve:

12 hour - probably need some variants on the am/pm:
^(?:[0]?[0-9]|1[0-2]):[0-5][0-9][ap]m$

24 hour with two digit hour:
^(?:[0-1][0-9]|[2][0-3]):[0-5][0-9]$

To get the dates correct with the number of days and such is much more complicated.  Your simple expressions will validate the formats but won't tell you if the values are legal.
Assisted Solution
 
08.07.2007 at 04:13PM PDT, ID: 19650332
I liked Arthur Wood's solution the best, as IS more VB-ish.

Drichards offered a perfectly valid alternative, and pointed out the typo - something that I should have caught quickly - had I been looking in the write place to begin with.  Also, his suggestion re the regex'es, while not really a part of the question, will be useful.  I'll probably be using the "IsDate" function to test for it to be a valid date AFTER it matches the regex -- I'm not a regex master by any means, and I'm not planning on trying to come up with one that takes the different days of the month or leap years into accout.

Thanks, Guys.

Kevin

P.S. - it ends up like the points split after all! :-)
 
 
12.06.2007 at 01:42PM PST, ID: 20423594
I always use IsDate(txtString) in a QA step before using the values.

If Not IsDate("User entered stuff") then
  ... err message
 
 
 
20080236-EE-VQP-29 / EE_QW_1_20070628