Solved

Convert String to date

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Crystal reports login after closing form 3 36
orderby list (from Json) 1 30
VB.net Filesystem watcher not working 5 37
VB.net/VSTO Excel Add-in 2 19
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

733 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