Date format pattern for "03rd of April 2006" type date

  • Thread starter Thread starter zsolt
  • Start date Start date
Z

zsolt

Hi,

I'm trying to convert a string to date by specifying the format. The value
is like this: "03rd of April 2006". Now this is converted fine by using the
following format: "dd\r\d \o\f MMMM yyyy". The problem is of course that the
string can be 01st of..., 04th of etc., meaning that I can't use the "\r\d"
format description for all possible values.

Any suggestions? Is there a wildcard i can use, like "dd\?\?"...

Thanks,
Zsolt
 
If you find no better way, try something like this:

switch (theDateTime.Day)
{
case 1:
// use "st" here
case 2:
// use "nd" here
case 3:
// use "rd" here
default:
// use "th" here
}

Let's write a private helper for this.

Hope it helps,
Thi
 
Hi Thi,

Thanks for the suggestion but I would really like not to do that :).

format = new System.Globalization.CultureInfo(CultureInfo.InvariantCulture.LCID, false);

System.DateTime DateValue = System.DateTime.ParseExact(inputString, formatDescription, format);

Now the inputString is "03rd of April 2006" and the user have to tell me/system what the formatDescription is. It can't give me the "rd" hardcoded format, so question is if the format can be specified to cover the "st", "nd" etc cases, or we have to have something that you suggest and have to parse the format and the inputString and figure out the final format.

Regards,

Zsolt
 
zsolt said:
Hi,

I'm trying to convert a string to date by specifying the format. The value
is like this: "03rd of April 2006". Now this is converted fine by using the
following format: "dd\r\d \o\f MMMM yyyy". The problem is of course that the
string can be 01st of..., 04th of etc., meaning that I can't use the "\r\d"
format description for all possible values.

Any suggestions? Is there a wildcard i can use, like "dd\?\?"...

It appears not. However, how about constructing a new string from the
first two characters of the input string, plus the ninth character
onwards, and then parsing that?
 
Hi Larry,
It appears not. However, how about constructing a new string from the
first two characters of the input string, plus the ninth character
onwards, and then parsing that?

Yes, that is possible and it seems that it has to be done that way. The
problem is that the input format can be anything from 2006.04.03 to the
mentioned one. Because of that, I have to find out if the format is the
problematic one and make the replace/extraction or just leave it the user's
way.

It just feels strange that it's not handled easier by the framework - it's
not that unusual format. Or maybe it is :)
 
I don't think there is some other way. You can write another Parse
method to parse yourself. One way is trying to remove the "rd" before
parse, something like this;

DateTime ParseDateTime(string input)
{
string s = input.SubString(0, 2) + input.SubString(4);
// now s is "03 of April 2006", you can parse it as normal
}

It is exactly what Larry suggested.
 
zsolt said:
Hi Larry,


Yes, that is possible and it seems that it has to be done that way. The
problem is that the input format can be anything from 2006.04.03 to the
mentioned one. Because of that, I have to find out if the format is the
problematic one and make the replace/extraction or just leave it the user's
way.

It just feels strange that it's not handled easier by the framework - it's
not that unusual format. Or maybe it is :)

I think I have written code in every language I have ever programmed
professionally, to produce the correct 'st' 'nd' 'th' suffix. The
Framework seems to be no exception, in that there doesn't seem to be a
way to produce these suffixes automatically; these are the only
datetime format patterns relating to days:

d The day of the month. Single-digit days will not have a leading zero.

dd The day of the month. Single-digit days will have a leading zero.
ddd The abbreviated name of the day of the week, as defined in
AbbreviatedDayNames.
dddd The full name of the day of the week, as defined in DayNames.

If it were up to me, there would definitely be a way of producing '1st'
etc, especially as this (like many of the format patterns) depends on
culture.
 
Hi,

Yes, that is possible and it seems that it has to be done that way. The
problem is that the input format can be anything from 2006.04.03 to the
mentioned one. Because of that, I have to find out if the format is the
problematic one and make the replace/extraction or just leave it the
user's way.

Can you control the format?
If not you have more than one problem at hand, first you have to see what
format you getting the date from , and sometimes it's difficult to know, a
good example 01/02/03 it can be viewed as 2nd of Jan 2003 ( in english) or
1st of feb ( in spanish)
It just feels strange that it's not handled easier by the framework - it's
not that unusual format. Or maybe it is :)

I find the particular example weird, it's the first ime I see it
 
Can you control the format?
If not you have more than one problem at hand, first you have to see what
format you getting the date from , and sometimes it's difficult to know,
a good example 01/02/03 it can be viewed as 2nd of Jan 2003 ( in english)
or 1st of feb ( in spanish)

Yes, the format is controled - the user is forced to describe the format.
The post is basically how to describe the "03rd of April 2006". The
"01/02/03" is easy when you know the format, the problem is only when you
try to guess the date without having format info. In my case I know the
format but can't describe it so the .NET understands it...
 
This is where C# is really lacking as a language. You should be able to
replace the Parse/ParseExact method of the DateTime class without
subclassing or requiring helper classes.

-Alan
 
Larry Lard said:
If it were up to me, there would definitely be a way of producing '1st'
etc, especially as this (like many of the format patterns) depends on
culture.

I think that you hit the nail on hte head. It's all about cultrue, and what
if your db is in London and your client is accesing in Rome, or worse yet in
China?
 
Back
Top