?
Solved

vb.net string to date conversion

Posted on 2014-10-12
9
Medium Priority
?
674 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 15

Assisted Solution

by:ChloesDad
ChloesDad earned 600 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 1400 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

765 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