Solved

Convert String to date

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

827 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