Bug in System.Globalization.Calendar class?

  • Thread starter Thread starter greyseal
  • Start date Start date
G

greyseal

Consider the following:

As I post this, it is June 11th, 2004.
My desk calendar tells me it is day 163 of the current year.

This code:

Calendar myCal = CultureInfo.InvariantCulture.Calendar;
DateTime theDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month,
DateTime.Now.Day, new GregorianCalendar() );
string dateBack = theDate.ToString("D") + " (" +
myCal.GetDayOfYear(theDate).ToString() + ")";

returns Friday, June 11, 2004 (150)

Obviously, the day of the year is incorrect. What am I doing wrong?

TIA.

greyseal
 
greyseal,
Your code converted to VB.NET produces:

dateBack "Friday, June 11, 2004 (163)" String

Under VB.NET 2003 on Windows XP Pro with regional settings for the U.S.

Dim myCal As Calendar = CultureInfo.InvariantCulture.Calendar
Dim theDate As DateTime = New DateTime(DateTime.Now.Year,
DateTime.Now.Month, DateTime.Now.Day, New GregorianCalendar)
Dim dateBack As String = theDate.ToString("D") + " (" +
myCal.GetDayOfYear(theDate).ToString() + ")"

Just curious why do you get the InvariantCulture's Calendar then pass New
GregorianCalendar to DateTime? I would expect you should use the same
calendar in both cases!

Hope this helps
Jay
 
greyseal said:
Consider the following:

As I post this, it is June 11th, 2004.
My desk calendar tells me it is day 163 of the current year.

This code:

Calendar myCal = CultureInfo.InvariantCulture.Calendar;
DateTime theDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month,
DateTime.Now.Day, new GregorianCalendar() );
string dateBack = theDate.ToString("D") + " (" +
myCal.GetDayOfYear(theDate).ToString() + ")";

returns Friday, June 11, 2004 (150)

Obviously, the day of the year is incorrect. What am I doing wrong?

Not sure - when I run the code, I get back the correct answer.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
The InvariantCulture and Gregorian references slipped in as I tried
different options to make the dang thing work (see
System.Globilization.Calendar).

I finally got back to the project today and decided to try the
following:

dateBack = theDate.ToString("D") + " (" + theDate.DayOfYear + ")";

Worked like a champ - how'd I miss that?

Go figure.

greyseal
 
Back
Top