473,378 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 17584
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 which is filled everytime a visitor enters the site....
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) and 14 instead of 2:00:00 PM etc.. If...
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 (DAY(itemdate) = '02') and (YEAR(itemdate) = '2005') order...
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: DatePart("m",) + DatePart("d",) +...
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 system. Bookings can only be made in weekly...
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 getting a number for the month even if the field...
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. Im wondering why, bcoz the 2 other shift works...
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 have 2 drop down boxes to select the listedmonth...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.