By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,671 Members | 1,559 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,671 IT Pros & Developers. It's quick & easy.

Unable to convert string to date

P: n/a
Hello All,

I wrote the following program.

using System;
using System.Globalization;

public class ShortDateTest
{
public static void Main(string[] args)
{
string date = @"26/04/1974";
DateTimeFormatInfo dtInfo = new DateTimeFormatInfo();
dtInfo.ShortDatePattern = @"dd/mm/yyyy";
DateTime birthDay = Convert.ToDateTime(date, dtInfo);
Console.WriteLine(birthDay.ToString());
}
}

But it throws an exception saying

Unhandled Exception: System.FormatException: Could not determine the
order of ye
ar, month, and date from dd/mm/yyyy.
at System.DateTimeParse.GetYearMonthDayOrder(String datePattern,
DateTimeForm
atInfo dtfi)
at System.DateTimeParse.GetDayOfNNY(DateTimeResult result,
DateTimeRawInfo ra
w, DateTimeFormatInfo dtfi)
at System.DateTimeParse.ProcessTerminaltState(Int32 dps,
DateTimeResult resul
t, DateTimeRawInfo raw, DateTimeFormatInfo dtfi)
at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi,
DateTimeStyl

Why I am not able to parse this date? I have used this same methodology
successfully to parse dates of pattern dd-MMM-yy. So why doesn't it work
for dd/mm/yyyy?

thanks for your help in advance.

regards,
Abhishek.
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
IIRC mm is minutes. Try a pattern of

@"dd/MM/yyyy"

HTH
Brian W

"Abhishek Srivastava" <ab*****************@nospam.net> wrote in message
news:Ou**************@TK2MSFTNGP12.phx.gbl...
Hello All,

I wrote the following program.

using System;
using System.Globalization;

public class ShortDateTest
{
public static void Main(string[] args)
{
string date = @"26/04/1974";
DateTimeFormatInfo dtInfo = new DateTimeFormatInfo();
dtInfo.ShortDatePattern = @"dd/mm/yyyy";
DateTime birthDay = Convert.ToDateTime(date, dtInfo);
Console.WriteLine(birthDay.ToString());
}
}

But it throws an exception saying

Unhandled Exception: System.FormatException: Could not determine the
order of ye
ar, month, and date from dd/mm/yyyy.
at System.DateTimeParse.GetYearMonthDayOrder(String datePattern,
DateTimeForm
atInfo dtfi)
at System.DateTimeParse.GetDayOfNNY(DateTimeResult result,
DateTimeRawInfo ra
w, DateTimeFormatInfo dtfi)
at System.DateTimeParse.ProcessTerminaltState(Int32 dps,
DateTimeResult resul
t, DateTimeRawInfo raw, DateTimeFormatInfo dtfi)
at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi,
DateTimeStyl

Why I am not able to parse this date? I have used this same methodology
successfully to parse dates of pattern dd-MMM-yy. So why doesn't it work
for dd/mm/yyyy?

thanks for your help in advance.

regards,
Abhishek.

Nov 15 '05 #2

P: n/a
Yao
I think you should change the

dtInfo.ShortDatePattern = @"dd/mm/yyyy"; to

dtInfo.ShortDatePattern = @"dd/MM/yyyy";

the Month character must be capitalized.
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.