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
Solved

vb.net string to date conversion

Posted on 2014-10-12
9
647 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
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.

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
VB.Net Report Printing Issue 3 51
SQL LINE CONTINUATION ISSUE 12 37
Write to a printer using vb.net 9 38
Access Schema 6 21
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
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…

791 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