C# DatePart() ?

M

Mika M

Hi!

I'm converting some methods of VB-class into C#-class for another
project. It's quite easy, but when converting method which returns last
week number of the entered year I got problems. The VB code is...

Private Function GetLastWeek(ByVal year As Integer) As Integer
'// Get last day of the year
Dim dte As DateTime = New DateTime(year, 12, 31)
'// Return last day weeknumber of the year
Return dte.Year * 100 + DatePart(DateInterval.WeekOfYear, dte,
FirstDayOfWeek.Monday, FirstWeekOfYear.System)
End Function

How to do this using C# because the following code below is not working
because "DatePart" is not available in C#?

private int GetLastWeek(int year)
{
// Get last day of the year
DateTime d = new DateTime(year, 12, 31);
// Return last day weeknumber of the year
return d.Year * 100 + DatePart(DateInterval.WeekOfYear, d,
FirstDayOfWeek.Monday, FirstWeekOfYear.System);
}
 
P

Peter Duniho

[...]
How to do this using C# because the following code below is not working
because "DatePart" is not available in C#?

I think you are looking for the Calendar class. For example:

private int GetLastWeek(int year)
{
// Get last day of the year
DateTime d = new DateTime(year, 12, 31);
// Return last day weeknumber of the year
return d.Year * 100 +
GregorianCalendar.GetWeekOfYear(d,
DateTimeFormatInfo.Current.CalendarWeekRule, DayOfWeek.Monday);
}

The above explicitly uses the Gregorian calendar, but you could also use
CurrentCulture.Calendar if you wanted for some reason to handle alternate
calendars.

Pete
 
J

Jon Skeet [C# MVP]

[...]
How to do this using C# because the following code below is not working
because "DatePart" is not available in C#?

I think you are looking for the Calendar class. For example:

private int GetLastWeek(int year)
{
// Get last day of the year
DateTime d = new DateTime(year, 12, 31);
// Return last day weeknumber of the year
return d.Year * 100 +
GregorianCalendar.GetWeekOfYear(d,
DateTimeFormatInfo.Current.CalendarWeekRule, DayOfWeek.Monday);
}

Small corrections - GetWeekOfYear isn't static, and there's no Current
property in DateTimeFormatInfo:

private static int GetLastWeek(int year)
{
// Get last day of the year
DateTime d = new DateTime(year, 12, 31);
// Return last day weeknumber of the year
GregorianCalendar calendar = new GregorianCalendar();
return d.Year * 100 +
calendar.GetWeekOfYear(d,
DateTimeFormatInfo.CurrentInfo.CalendarWeekRule,
DayOfWeek.Monday);
}

Jon
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Jon said:
[...]
How to do this using C# because the following code below is not working
because "DatePart" is not available in C#?
I think you are looking for the Calendar class. For example:

private int GetLastWeek(int year)
{
// Get last day of the year
DateTime d = new DateTime(year, 12, 31);
// Return last day weeknumber of the year
return d.Year * 100 +
GregorianCalendar.GetWeekOfYear(d,
DateTimeFormatInfo.Current.CalendarWeekRule, DayOfWeek.Monday);
}

Small corrections - GetWeekOfYear isn't static, and there's no Current
property in DateTimeFormatInfo:

private static int GetLastWeek(int year)
{
// Get last day of the year
DateTime d = new DateTime(year, 12, 31);
// Return last day weeknumber of the year
GregorianCalendar calendar = new GregorianCalendar();
return d.Year * 100 +
calendar.GetWeekOfYear(d,
DateTimeFormatInfo.CurrentInfo.CalendarWeekRule,
DayOfWeek.Monday);
}

Jon

You should probably check the week number before using it. For some week
rules the last day of the year is not always in the last week of the
year, as the week belongs to the next year if it has three or less days
in the previous year. If the week number is one, you would have to
subtract seven days from the date to get to the previous week and get
the week number for that date instead.
 
N

Nicholas Paldino [.NET/C# MVP]

Mika,

While the other suggestions are valid, why not just use the static
DatePart method on the DateAndTime class in the Microsoft.VisualBasic
namespace? Just set a reference to Microsoft.VisualBasic.dll, and off you
go. The easiest code to maintain is code you don't have to write, and this
classifies as code you don't have to write.
 
P

Peter Duniho

You should probably check the week number before using it. For some week
rules the last day of the year is not always in the last week of the
year, as the week belongs to the next year if it has three or less days
in the previous year. If the week number is one, you would have to
subtract seven days from the date to get to the previous week and get
the week number for that date instead.

That's true, but that bug exists in the original code too. We're just
trying to help the guy port his code, bugs and all. :)

(Actually, the above is only half tongue-in-cheek...there are actually
very good real reasons sometimes to make sure that old bugs remain).
 
P

Peter Duniho

While the other suggestions are valid, why not just use the static
DatePart method on the DateAndTime class in the Microsoft.VisualBasic
namespace? Just set a reference to Microsoft.VisualBasic.dll, and off
you go. The easiest code to maintain is code you don't have to write,
and this classifies as code you don't have to write.

Well, one reason I can think of is that if there's not already a reference
to VB and it's not loaded, then adding the reference just for one call,
especially when similar functionality already exists in the .NET Framework
classes which are already loaded, could be considered wasteful.

Pete
 
N

Nicholas Paldino [.NET/C# MVP]

Could be, but honestly, I would file this under "premature
optimization".
 
P

Peter Duniho

Could be, but honestly, I would file this under "premature
optimization".

Well, if he weren't already in the process of actually trying to port a
bunch of scripting VB code to C#, I could see that point. But he is. I
got the impression that the whole point of the project was to move
everything over to C#.

One solution, the one requiring the least effort, would be to just leave
all of the original VB intact, referencing it from C# and calling it as
needed. That's even easier than borrowing VB methods and classes via
reference into the C# module.

It's not really that hard to do what he wants in C#, so it seems to me
that actually changing the text of the method so that it calls something
that is already supported from within C# via the .NET Framework is no more
work, and more efficient.

I agree wholeheartedly that one shouldn't waste time on optimizations
until one knows the code needs optimizing. But I don't see much if any of
a waste of time here. The effort seems about the same one way or the
other.

Pete
 
M

Mika M

I finally find it up by myself...

public int GetYearWeek(DateTime time, string cultureInfo)
{
CultureInfo cul = new CultureInfo(cultureInfo);
return time.Year * 100 + cul.Calendar.GetWeekOfYear(time,
cul.DateTimeFormat.CalendarWeekRule, cul.DateTimeFormat.FirstDayOfWeek);
}

....why help-systems and answers are so complex these days to find easily
something useful for tiny and regularly needed tasks like this!!! Just
this little code and that's it!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top