Solved

Convert String to date

Posted on 2014-11-18
5
283 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Angular JS Route 3 54
How would you add MULTITHREADING to the attached C# code? 4 52
Run time Error 4 34
Close tabpage of custom control 9 23
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

896 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

13 Experts available now in Live!

Get 1:1 Help Now