Solved

Convert String to date

Posted on 2014-11-18
5
314 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
[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
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

690 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