Convert to DateTime
-
Hi, I need to convert string in specific format to DateTime structure. I tryed following code but it throws exception.
string text = "4113013050"; string format = "YMMddHmmss"; DateTime dateTime = DateTime.ParseExact(text,format,null) ; textBox1.Text = dateTime.ToString();
Can somebody tell me what am I doing wrong. Thanks Salut! -
Hi, I need to convert string in specific format to DateTime structure. I tryed following code but it throws exception.
string text = "4113013050"; string format = "YMMddHmmss"; DateTime dateTime = DateTime.ParseExact(text,format,null) ; textBox1.Text = dateTime.ToString();
Can somebody tell me what am I doing wrong. Thanks Salut!Hi, The error you got is : String was not recognised as a valid date. What I had to do is the reformat the string in this string : string text = "2004-11-30 13:05:01"; I'm not sure from where the text value comes. But you could use other wyas to do this.
-
Hi, I need to convert string in specific format to DateTime structure. I tryed following code but it throws exception.
string text = "4113013050"; string format = "YMMddHmmss"; DateTime dateTime = DateTime.ParseExact(text,format,null) ; textBox1.Text = dateTime.ToString();
Can somebody tell me what am I doing wrong. Thanks Salut!Hi, you could do it with the Converter class instead:
Convert.ToDateTime(string yourString);
or with a customised formatConvert.ToDateTime(string yourString, System.IFormatProvider yourFormat);
Cheers, sommarafton -
Hi, I need to convert string in specific format to DateTime structure. I tryed following code but it throws exception.
string text = "4113013050"; string format = "YMMddHmmss"; DateTime dateTime = DateTime.ParseExact(text,format,null) ; textBox1.Text = dateTime.ToString();
Can somebody tell me what am I doing wrong. Thanks Salut!That format will never work and I would be surprised if u can find anyway to parse it. Problems: 1: Years cannot be specified by one digit. How is the computer suposed to know if its 2004, or 2014 or even 1914. U need at least 2 digits and better if u use all 4 (2000 should have taught us that by now) 2: Same happens with the hours. No matter if u use the 12 hour AM/PM format or 24 hours, time can get to two digit values (11 PM / 23). Ur parsing would work if u can get ur date to follow a "YYYYMMddHHmmss" pattern and will probably also work with a "YYMMddHHmmss" pattern although i'd go for first.