Solved

vb.net string to date conversion

Posted on 2014-10-12
9
642 Views
Last Modified: 2014-10-13
Hey guys,
This should be really simple, but so often I have problems working with date/time.

I have a string in the format "Sun, 12 Oct 2014 11:33:18 GMT+8"
I need to save this in a SQL database with the datatype "datetime".

In VB.NET I've tried to convert the string to date by using Date.ParseExact, but I keep getting the message "A first chance exception of type 'System.Data.SqlTypes.SqlTypeException' occurred in System.Data.dll"

This is what I tried:
Dim Timestamp As Date
Dim Format As String = "ddd, dd MMM yyyy hh:mm:tt zzz"
Dim provider As CultureInfo = CultureInfo.InvariantCulture

Timestamp = Date.ParseExact(DateString, Format, provider)

I tried with a few variations, with and without the comma after ddd, with 3 or 5 zzz in the end etc.

What am I doing wrong here?
0
Comment
Question by:VikingOnline
9 Comments
 
LVL 15

Assisted Solution

by:ChloesDad
ChloesDad earned 150 total points
ID: 40376138
Firstly your format is wrong, you are using  tt for seconds, it should be ss.

Secondly to use zzz the offset must be in hh:mm format, GMT is not required an option, zz means two digit hour (i.e. leading zero required), z means 1 or 2 digit digit hour

        Dim Timestamp As Date
        Dim Format As String = "ddd, dd MMM yyyy HH:mm:ss zz"
        Dim provider As System.Globalization.CultureInfo = System.Globalization.CultureInfo.InvariantCulture

        Timestamp = Date.ParseExact("Sun, 12 Oct 2014 11:33:18 +08", Format, provider)

Open in new window


You can also use TryParseExact, which will return NOTHING rather than cause an exception.
0
 

Author Comment

by:VikingOnline
ID: 40376207
Hi ChloesDad,
Wow, I knew :ss and not :tt, but missed it. (I'm always messing around with date things, so I copied from msdn and adjusted - did not notice it was tt and not ss.)

I updated to the format you posted (Dim Format As String = "ddd, dd MMM yyyy HH:mm:ss zz"), but I'm still getting the error, though.

I did a debug.print for the date string to be sure something did not go wrong before it.
And I've not tried TryParseExact, but I put all of it in a Try, Catch block and the FormatException I get is also below.
Debug.Print("Date String: " & DateString)

Date String: Sun, 12 Oct 2014 16:46:48 GMT+8
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
Exception: System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)
   at System.DateTime.ParseExact(String s, String format, IFormatProvider provider)

And it's full stop by the time it gets to the SQL code.
A first chance exception of type 'System.Data.SqlTypes.SqlTypeException' occurred in System.Data.dll
0
 

Author Comment

by:VikingOnline
ID: 40376215
So it's probably not the right way to do it, but it worked when I used substring to remove the GMT+8 and also removed the zz from the format.

Dim Format As String = "ddd, dd MMM yyyy HH:mm:ss"
Dim provider As CultureInfo = CultureInfo.InvariantCulture

DateString = DateString.Substring(0,DateString.Length - 6)
TimeStamp = Date.ParseExact(DateString, Format, provider)
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 40
ID: 40376234
Have you tried Parse instead of ParseExact? Parse usually works with any valid date without having to specify the format.
0
 

Author Comment

by:VikingOnline
ID: 40376295
Yeah, for me it did not recognize it as a valid DateTime if I just used Parse.

A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
Exception: System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at System.DateTime.Parse(String s)
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 40376451
If you are not providing a leading zero ie +8 rather than +08 then its just 1 z not two.
0
 
LVL 11

Accepted Solution

by:
louisfr earned 350 total points
ID: 40376477
If you absolutely need your input string as you showed, you need to include the 'GMT' in the parse format:
"ddd, dd MMM yyyy hh:mm:ss 'GMT'z"
0
 

Author Comment

by:VikingOnline
ID: 40376553
The input sting comes from a system I can't change, so that's fixed.
However I can, of course, change it with the substring solution and remove the GMT part. I just did not think that was the right/best solution.

I tried the suggested "ddd, dd MMM yyyy hh:mm:ss 'GMT'z", and that works!

Thanks for all your help!
0
 

Author Closing Comment

by:VikingOnline
ID: 40376557
Thanks for your help.
I knew it should be a simple solution, but I would not have thought of actually putting 'GMT' into it. But when I see it, it's so obvious.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question