Solved

vb.net string to date conversion

Posted on 2014-10-12
9
620 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
Have you tried Parse instead of ParseExact? Parse usually works with any valid date without having to specify the format.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:VikingOnline
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now