parsing a string with DateTime.ParseExact
-
Hi. I am having trouble parsing a string into the format I want. Basically, I want the string to end up being "MMdd" where MM is the month (with a leading zero if necessary) and dd is the day (with a leading zero if necessary). This is what I have tried: String today = DateTime.Today.Date.ToShortDateString(); String pattern = "MMdd"; DateTime dt_today = DateTime.ParseExact(today, pattern, null); // line 3 today = dt_today.ToString(); Console.WriteLine("today's date is: " + today); The problem is that my program crashes once run. If you look at the third line, in place of null, I have tried sending: System.Globalization.CultureInfo.InvariantCulture, System.Globalization.CultureInfo.CurrentCulture Both yielded the same result. I have also tried using "Now" instead of "Today" in the first line. What am I doing wrong? I'd appreciate any help.
-
Hi. I am having trouble parsing a string into the format I want. Basically, I want the string to end up being "MMdd" where MM is the month (with a leading zero if necessary) and dd is the day (with a leading zero if necessary). This is what I have tried: String today = DateTime.Today.Date.ToShortDateString(); String pattern = "MMdd"; DateTime dt_today = DateTime.ParseExact(today, pattern, null); // line 3 today = dt_today.ToString(); Console.WriteLine("today's date is: " + today); The problem is that my program crashes once run. If you look at the third line, in place of null, I have tried sending: System.Globalization.CultureInfo.InvariantCulture, System.Globalization.CultureInfo.CurrentCulture Both yielded the same result. I have also tried using "Now" instead of "Today" in the first line. What am I doing wrong? I'd appreciate any help.
I'd use an overload of the ToString method on the datetime, which allows you to specify the format. This looks like too many steps to take, to me.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Hi. I am having trouble parsing a string into the format I want. Basically, I want the string to end up being "MMdd" where MM is the month (with a leading zero if necessary) and dd is the day (with a leading zero if necessary). This is what I have tried: String today = DateTime.Today.Date.ToShortDateString(); String pattern = "MMdd"; DateTime dt_today = DateTime.ParseExact(today, pattern, null); // line 3 today = dt_today.ToString(); Console.WriteLine("today's date is: " + today); The problem is that my program crashes once run. If you look at the third line, in place of null, I have tried sending: System.Globalization.CultureInfo.InvariantCulture, System.Globalization.CultureInfo.CurrentCulture Both yielded the same result. I have also tried using "Now" instead of "Today" in the first line. What am I doing wrong? I'd appreciate any help.
Hi, if you want your ParseExact to succeed, today must consist of exactly four digits. So it all depends on your regional settings, which control the outcome of ToShortDateString(). You could try
String today = DateTime.Today.Date.ToString("MMdd");
:)Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google
-
Hi. I am having trouble parsing a string into the format I want. Basically, I want the string to end up being "MMdd" where MM is the month (with a leading zero if necessary) and dd is the day (with a leading zero if necessary). This is what I have tried: String today = DateTime.Today.Date.ToShortDateString(); String pattern = "MMdd"; DateTime dt_today = DateTime.ParseExact(today, pattern, null); // line 3 today = dt_today.ToString(); Console.WriteLine("today's date is: " + today); The problem is that my program crashes once run. If you look at the third line, in place of null, I have tried sending: System.Globalization.CultureInfo.InvariantCulture, System.Globalization.CultureInfo.CurrentCulture Both yielded the same result. I have also tried using "Now" instead of "Today" in the first line. What am I doing wrong? I'd appreciate any help.
What exactly are you trying to do? Do you already have a
DateTime
object that you are trying to display in a specific format? If so, you just need to callToString
on that object and pass the format string you want. The code you show takes aDateTime
object, converts it to a string, and then tries to parse that string and convert it back to aDateTime
object, which you then callToString
on to turn it back into a string. You can accomplish the same thing by doing this:Console.WriteLine("today's date is: " + DateTime.Today.ToString("MMdd"));
Scott.
—In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]
-
Hi. I am having trouble parsing a string into the format I want. Basically, I want the string to end up being "MMdd" where MM is the month (with a leading zero if necessary) and dd is the day (with a leading zero if necessary). This is what I have tried: String today = DateTime.Today.Date.ToShortDateString(); String pattern = "MMdd"; DateTime dt_today = DateTime.ParseExact(today, pattern, null); // line 3 today = dt_today.ToString(); Console.WriteLine("today's date is: " + today); The problem is that my program crashes once run. If you look at the third line, in place of null, I have tried sending: System.Globalization.CultureInfo.InvariantCulture, System.Globalization.CultureInfo.CurrentCulture Both yielded the same result. I have also tried using "Now" instead of "Today" in the first line. What am I doing wrong? I'd appreciate any help.
-
I like how all the replies say the same thing :laugh:
My current favourite word is: Waffle Cheese is still good though.