Solved

vb.net string to date conversion

Posted on 2014-10-12
9
660 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 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

737 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