DateTimeFormatInfo, Culture and custom formatting

  • Thread starter Thread starter George Ionescu
  • Start date Start date
G

George Ionescu

Hello all,

given the following piece of code:

DateTimeFormatInfo dfi = (new CultureInfo("Ro-ro")).DateTimeFormat;
DateTime now = DateTime.Now;
string strDate = now.ToString("dd-MMM-yyyy", dfi);

the strDate contains the string '21-iul.-2004'

What's with the dot after the month's name ? Am I doing something wrong here
?

Thank you.
Regards,
George Ionescu
 
Hey George,

I guess this is how Microsoft thinks that dates are formatted in Romania :-) If you do not like this format you can change it using DateTimeFormatInfo.AbbreviatedMonthNames:

DateTimeFormatInfo dfi = (new CultureInfo("Ro-ro")).DateTimeFormat;
DateTime now = DateTime.Now;
dfi.AbbreviatedMonthNames = new string[13] { "ian", "feb", "mar", "apr", "mai", "iun", "iul", "aug", "sep", "oct", "nov", "dec", "" };
string strDate = now.ToString("dd-MMM-yyyy", dfi);

Regards, Jakob.
 
Hello Jakob,

Thanks, that worked. One other question: I'd like to support different date
separators (e.g. dot, comma, slash); what should I use to parse a string and
construct a date? Should I go for regular expressions or is there another
way (date.ParseExact only allows one format).

Thank you.
Regards,
George Ionescu

Jakob Christensen said:
Hey George,

I guess this is how Microsoft thinks that dates are formatted in Romania
:-) If you do not like this format you can change it using
DateTimeFormatInfo.AbbreviatedMonthNames:
DateTimeFormatInfo dfi = (new CultureInfo("Ro-ro")).DateTimeFormat;
DateTime now = DateTime.Now;
dfi.AbbreviatedMonthNames = new string[13] { "ian", "feb", "mar", "apr",
"mai", "iun", "iul", "aug", "sep", "oct", "nov", "dec", "" };
 
Hey George,

I don't think there is an easy way to do this. Regular expressions may be the best solution. You can also use the property DateTimeFormatInfo.DateSeparator to specify which date separator you wish to use. But I don't think there is any built-in way for supporting more that one separator unless you want to try parsing the string with a number of different separators using DateTimeFormatInfo.DateSeparator.

Regards, Jakob.


George Ionescu said:
Hello Jakob,

Thanks, that worked. One other question: I'd like to support different date
separators (e.g. dot, comma, slash); what should I use to parse a string and
construct a date? Should I go for regular expressions or is there another
way (date.ParseExact only allows one format).

Thank you.
Regards,
George Ionescu

Jakob Christensen said:
Hey George,

I guess this is how Microsoft thinks that dates are formatted in Romania
:-) If you do not like this format you can change it using
DateTimeFormatInfo.AbbreviatedMonthNames:
DateTimeFormatInfo dfi = (new CultureInfo("Ro-ro")).DateTimeFormat;
DateTime now = DateTime.Now;
dfi.AbbreviatedMonthNames = new string[13] { "ian", "feb", "mar", "apr",
"mai", "iun", "iul", "aug", "sep", "oct", "nov", "dec", "" };
string strDate = now.ToString("dd-MMM-yyyy", dfi);

Regards, Jakob.
 
Thanks Jakob.

I'll go for RegEx, then.

Regards,
George Ionescu

Jakob Christensen said:
Hey George,

I don't think there is an easy way to do this. Regular expressions may be
the best solution. You can also use the property
DateTimeFormatInfo.DateSeparator to specify which date separator you wish to
use. But I don't think there is any built-in way for supporting more that
one separator unless you want to try parsing the string with a number of
different separators using DateTimeFormatInfo.DateSeparator.
Regards, Jakob.


George Ionescu said:
Hello Jakob,

Thanks, that worked. One other question: I'd like to support different date
separators (e.g. dot, comma, slash); what should I use to parse a string and
construct a date? Should I go for regular expressions or is there another
way (date.ParseExact only allows one format).

Thank you.
Regards,
George Ionescu

Jakob Christensen said:
Hey George,

I guess this is how Microsoft thinks that dates are formatted in
Romania
:-) If you do not like this format you can change it using
DateTimeFormatInfo.AbbreviatedMonthNames:
DateTimeFormatInfo dfi = (new CultureInfo("Ro-ro")).DateTimeFormat;
DateTime now = DateTime.Now;
dfi.AbbreviatedMonthNames = new string[13] { "ian", "feb", "mar",
"apr",
"mai", "iun", "iul", "aug", "sep", "oct", "nov", "dec", "" };
string strDate = now.ToString("dd-MMM-yyyy", dfi);

Regards, Jakob.


:

Hello all,

given the following piece of code:

DateTimeFormatInfo dfi = (new CultureInfo("Ro-ro")).DateTimeFormat;
DateTime now = DateTime.Now;
string strDate = now.ToString("dd-MMM-yyyy", dfi);

the strDate contains the string '21-iul.-2004'

What's with the dot after the month's name ? Am I doing something
wrong
here
?

Thank you.
Regards,
George Ionescu
 
Here are the 4 RE's that I have come up with to parse dates from a string.
These take into account different seperators and languagues that have
2 words for a month.

(\d+(?<mark>[-| |/|\.])[^\d|^ ]+ *[^\d|^ ]*\k<mark>\d+)
(\d+ *(?<mark>[ |/|\.|-]) *\d+ *\k<mark> *\d+)
([a-zA-Z]+[\.]* +\d+ *, *\d+)
([a-zA-Z]+[\.]* +[a-zA-Z]* *\d+ *, *\d+)

As with most things there is more than one way of doing it. This is what
worked for me.

You can take the results and pass it into the Parse() to see if it is a valid date.

try {
CultureInfo cinfo = new CultureInfo(name, false);
date = DateTime.Parse(REresultText, cinfo.DateTimeFormat);
}

Cheers,
Dave


Thanks Jakob.

I'll go for RegEx, then.

Regards,
George Ionescu

Jakob Christensen said:
Hey George,

I don't think there is an easy way to do this. Regular expressions may be
the best solution. You can also use the property
DateTimeFormatInfo.DateSeparator to specify which date separator you wish to
use. But I don't think there is any built-in way for supporting more that
one separator unless you want to try parsing the string with a number of
different separators using DateTimeFormatInfo.DateSeparator.
Regards, Jakob.


George Ionescu said:
Hello Jakob,

Thanks, that worked. One other question: I'd like to support different date
separators (e.g. dot, comma, slash); what should I use to parse a string and
construct a date? Should I go for regular expressions or is there another
way (date.ParseExact only allows one format).

Thank you.
Regards,
George Ionescu

Hey George,

I guess this is how Microsoft thinks that dates are formatted in Romania
:-) If you do not like this format you can change it using
DateTimeFormatInfo.AbbreviatedMonthNames:

DateTimeFormatInfo dfi = (new CultureInfo("Ro-ro")).DateTimeFormat;
DateTime now = DateTime.Now;
dfi.AbbreviatedMonthNames = new string[13] { "ian", "feb", "mar", "apr",
"mai", "iun", "iul", "aug", "sep", "oct", "nov", "dec", "" };
string strDate = now.ToString("dd-MMM-yyyy", dfi);

Regards, Jakob.


:

Hello all,

given the following piece of code:

DateTimeFormatInfo dfi = (new CultureInfo("Ro-ro")).DateTimeFormat;
DateTime now = DateTime.Now;
string strDate = now.ToString("dd-MMM-yyyy", dfi);

the strDate contains the string '21-iul.-2004'

What's with the dot after the month's name ? Am I doing something wrong
here
?

Thank you.
Regards,
George Ionescu
 
Back
Top