Solved

Convert String to date

Posted on 2014-11-18
5
273 Views
Last Modified: 2014-11-18
Hi Experts,

I have a date in string format. I want to display it in a date picker control. I am using the following code.

Dim Stk_Date As String = "20140713"
Dim dt As DateTime = DateTime.Parse((Stk_Date).ToString("yyyy-MM-dd"))

I am getting the error  "Unable to cast object of type "System.string" to type "system.IFormatProvider"  "

Thank you.
0
Comment
Question by:RadhaKrishnaKiJaya
  • 2
  • 2
5 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40450617
>>Dim Stk_Date As String = "20140713"

That is already a string, using the ToString doesn't make sense.  Where do you get this value of 20140713 from?
0
 
LVL 11

Expert Comment

by:louisfr
ID: 40450621
I think you misplaced a parenthesis:
Dim dt As DateTime = DateTime.Parse((Stk_Date)).ToString("yyyy-MM-dd")

Open in new window

0
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 40450647
Thank you for your reply. Date is in the database in string format.

Dim Stk_Date As String = Reader.Item("STK_DATE")
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 40450658
>>Date is in the database in string format.

OK, odd way to do it but OK.
The simplest would be to strip the string into the relevant parts assuming it is always 4 digit year, 2 digit month and 2 digit day as you have in your example.

Dim Stk_Date As String = "20140713"
Dim s2 As String = Stk_Date.Substring(0, 4) + "-" + Stk_Date.Substring(4, 2) + "-" + Stk_Date.Substring(6, 2)
Dim dt As DateTime = DateTime.Parse(s2)
0
 

Author Closing Comment

by:RadhaKrishnaKiJaya
ID: 40450797
It Worked!!! Thank you very much for your help.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

12 Experts available now in Live!

Get 1:1 Help Now