469,312 Members | 2,524 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,312 developers. It's quick & easy.

C# DatePart() ?

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);
}

--
Thanks in advance!

Mika
Jun 5 '07 #1
10 16660
On Tue, 05 Jun 2007 00:37:50 -0700, Mika M <ma***************@luukku.com
wrote:
[...]
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
Jun 5 '07 #2
On Jun 5, 9:42 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Tue, 05 Jun 2007 00:37:50 -0700, Mika M <mahmik_removet...@luukku.com>
wrote:
[...]
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

Jun 5 '07 #3
On Tue, 05 Jun 2007 02:02:24 -0700, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
Small corrections - GetWeekOfYear isn't static, and there's no Current
property in DateTimeFormatInfo:
Teach me to post so late. Thanks. :)
Jun 5 '07 #4
Jon Skeet [C# MVP] wrote:
On Jun 5, 9:42 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
>On Tue, 05 Jun 2007 00:37:50 -0700, Mika M <mahmik_removet...@luukku.com>
wrote:
>>[...]
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.

--
Gran Andersson
_____
http://www.guffa.com
Jun 5 '07 #5
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mika M" <ma***************@luukku.comwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
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);
}

--
Thanks in advance!

Mika
Jun 5 '07 #6
On Tue, 05 Jun 2007 02:37:00 -0700, Göran Andersson <gu***@guffa.com>
wrote:
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).
Jun 5 '07 #7
On Tue, 05 Jun 2007 06:57:59 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
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
Jun 5 '07 #8
Could be, but honestly, I would file this under "premature
optimization".

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Tue, 05 Jun 2007 06:57:59 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
> 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
Jun 5 '07 #9
On Tue, 05 Jun 2007 14:24:42 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
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
Jun 5 '07 #10
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!

--
Mika
Jun 6 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by David | last post: by
2 posts views Thread by Aaran76 | last post: by
2 posts views Thread by le0 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.