472,341 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,341 software developers and data experts.

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 17387
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.

--
Göran 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David | last post by:
Hi Group, i am trying to use the DatePart function on my SQL2000 database. I have a table called visitors with a field called DateTimeEntrance...
2
by: Tipple | last post by:
I'm trying to increment between the end of lunch and the end of the day. The code below is returning a datePart of 13 instead of 1 (for 1:00:00 PM)...
2
by: sdowney717 | last post by:
The field itemdate is a datetime field in sqlserver2000 DB This works fine: Select Id From BookData Where (MONTH(itemdate) = '01') and...
3
by: david liu | last post by:
i have a date (in date/time format with input mask) in access 2002. field date: 01/01/1970 what i want: 010170 (i.e. mmddyy) i used:...
2
by: Aaran76 | last post by:
I am creating a booking system in ASP.Net with VB and a MSSQL backend. I am having problems knowing where to begin with a particular part of the...
3
by: S. van Beek | last post by:
Dear reader, With DatePart() you can subtract the year or the week from a date field with: DatePart("yyyy";) for the year
2
by: troddy | last post by:
I am using the DatePart funtion in a query to extract the day, month and year in separate fields in a query. The function works fine but I am only...
2
by: le0 | last post by:
Hello guys, Is there anything wrong with my code (see below) bcoz when 10p-6a shift my browser returns the error cannot be a zero-length string....
3
by: grabit | last post by:
Hi Peoples I am having probs with the datepart function with a query.What i want is to get all listings made in any month in any year ie May 2007 i...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.