Solved

Get Week # between two dates

Posted on 2009-05-19
9
239 Views
Last Modified: 2012-05-07
Get Week # between two dates
DateTime d1;
DateTime d2;
DateTime d3;
 
d1=DateTime.Parse("04/01/09");
d2=DateTime.Parse("09/01/09");
d3=DateTime.Now;                      //e.g 05/19/09  
 
d3= what week # between d1 and d2 ???
 
URGENT.

Open in new window

0
Comment
Question by:gazens
  • 3
  • 3
  • 3
9 Comments
 
LVL 1

Expert Comment

by:kyleroi
ID: 24427977
Are you looking for numbers between the two dates or a date?  I am confused on what your trying to do.
0
 
LVL 9

Expert Comment

by:tculler
ID: 24427983
Week number as in, the first week of January is week one, and the last week of December is week 52? Or do you mean just how many weeks are between two given dates?
0
 

Author Comment

by:gazens
ID: 24428012
If I select current date i.e 05/19/2009. So between 1 to 52 weeks in a year what is the position of currentdate?  Is that good enough? Thanks.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 9

Accepted Solution

by:
tculler earned 500 total points
ID: 24428026
I guess that'll work, though I don't see the point of the other two dates there. This should be fairly simple:


public System.Int32 GetWeekNumber(System.DateTime myDate)
{
     System.Int32 weekNumber = myDate.DayOfYear / 7
     return (weekNumber == 0) ? weekNumber + 1 : weekNumber;
}

Open in new window

0
 
LVL 1

Expert Comment

by:kyleroi
ID: 24428100
public Int32 WeekOfYear(DateTime dtInput)
        {
            DateTime d1 = new DateTime(dtInput.Year, 01, 01);
            Int32 nCt = d1.DayOfWeek == DayOfWeek.Sunday ? 0 : 1;
            Boolean blFnd = false;
            while (d1.Year == dtInput.Year && blFnd == false)
            {
                if (d1.DayOfWeek == DayOfWeek.Sunday)
                {
                    nCt++;
                }
                if (d1.Date == dtInput.Date )
                {
                    blFnd = true;
                }
                else
                {
                    d1 = d1.AddDays(1);
                }
            }
            return nCt;
        }
0
 

Author Closing Comment

by:gazens
ID: 31583321
This is good enough.
0
 
LVL 1

Expert Comment

by:kyleroi
ID: 24428189
The accepted solution may not be accurate as the first week of any given year does not always have 7 days.   For example the first week this year only had 4 days.  Todays date is in the 21st calander week of the year but there has only been 19 full 7 day weeks.  

I guess it depends on what you are looking for.... good luck.
0
 

Author Comment

by:gazens
ID: 24428236
According to you : If I use your solution then its ok at the begining but when I put 12/28/09  then its shows 53 week is that true??
0
 
LVL 9

Expert Comment

by:tculler
ID: 24428264
The System.DateTime object should take care of any inconsistencies like that. Try the following code, for example; it even takes care of leap years and such. If I am wrong, and this solution isn't exactly what you're looking for, let me know.
for (int i = 1990; i < 2010; i++)
{
     Console.WriteLine("DayOfYear: {0}", new DateTime(i, 12, 30).DayOfYear);
}

Open in new window

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Icons and Colors for Terms 3 32
C#  Radio button search for Date not DateTime 4 46
Best resource to learn entity framework 2 32
FTP file download using c# 3 22
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question