C Programming Tutorials and advice
CProgrammingTrends News Archives About Us Feedback

Recent Articles


Creating Safe Events In C#
Whenever you write class libraries, custom control or just about anything else, you probably raise a lot of home made events. That's a simple thing to do, but tedious to write over and over again. That's why I always use...

Assigining a TypeConverter to a Class you Don't Own
I ran into problems with the XNA Beta1, where by I had a class that had a Vector2 struct in. The problem with the XNA Vector2 struct is that there is no TypeConverter...

Rational Betas Have C++ Goodies
Features in the Rational Software Architect v7.0 beta include improved support for C++, and developers can try out the open beta by pre-registering for it with IBM. Open betas for IBM Rational Software Architect V7.0...

Revisiting C/C++ On Eclipse
The tutorial from IBM's developerWorks on using the C/C++ Development Toolkit (CDT) for Eclipse received a refresh over the summer, and it merits a look from those who may have missed it since then.

Using Immediate Window to Work with Values
This article is an excerpt from the book: Murach's ASP.NET 2.0 Web Programming with C# 2005. The Immediate window, shown in figure 4-14, is useful for displaying the values of variables or properties that don't appear...

C++ - No Fears And Great Books
This is a quick look at a couple of stories on C++, namely why programmers should not feel The Fear of native code, and a quintet of books that should occupy the bookshelves of quality C++ coders. Tom Yager's...

New NetBeans Pack Supports C/C++
Developers who work in C or C++ can use the NetBeans integrated developer environment to build applications on Windows, Linux, and Solaris. NetBeans made the C/C++ Pack available as an open source project.


11.16.06


Working With Weeks In C# (Not That Obvious)


By Mads Kristensen

For some reason, Microsoft didn't add a Week property to the DateTime class. I never could figure out why.

Instead they gave us the System.Globalization namespace, filled with date related functionality like the different calendar classes. In this example, I'm going to use the GregorianCalendar class to find the week number of a certain date.



As you can see, it's not that difficult as long as you know where to look.

You can also check for the total number of weeks in a certain year. You would have to know the last day of the year, that never can be part of the first week of the following year. That day is december 28th. Here is a method that gives you the number of weeks in a specified year.



Now for the tricky part. Let's say you want to know the number of weeks between two dates. Just pass the two dates into the above method and subtract them from each other like "WeekNumber(dateFrom) - WeekNumber(dateTo) + 1". Wrong! If the first date is from another year than the second one, it will not work.

Low Rate eCommerce & Retail Plans

In order to write a method that takes two dates and return the number of weeks in between them, you need some smart logic to make it work. Here's is a method that I use for this purpose. It's simple to use, but a little too complex for such a simple task, which I think should have been a part of the DateTime class to begin with.



As you can see from these code examples, working with weeks in C# is not that obvious.

About the Author:
Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.

About CProgrammingTrends
A collection of articles and tutorials designed to help C and C variant programmers in their programming work. C Programming Tutorials and advice

CProgrammingTrends is brought to you by:

SecurityConfig.com NetworkingFiles.com
NetworkNewz.com WebProASP.com
DatabaseProNews.com SQLProNews.com
ITcertificationNews.com SysAdminNews.com
LinuxProNews.com WirelessProNews.com
CProgrammingTrends.com ITmanagmentNews.com


-- CProgrammingTrends is an iEntry, Inc. publication --
iEntry, Inc. 2549 Richmond Rd. Lexington KY, 40509
2006 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal


archives | advertising info | news headlines | free newsletters | comments/feedback | submit article


">Unsubscribe from CProgrammingTrends.
To unsubscribe from CProgrammingTrends or any other iEntry publication, simply send an email request to: support@ientry.com
CProgrammingTrends Home Page About Article Archive News Downloads WebProWorld Forums Jayde iEntry Advertise Contact