Click here to Skip to main content
15,895,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How to find number of weeks for between two different years. the value should be display in dropdown in asp.net.

pls. help.

Regards,
Ganesh.S
Posted

DateTime oldDate = new DateTime(2002,7,15);
DateTime newDate = DateTime.Now;

// Difference in days, hours, and minutes.
TimeSpan ts = newDate - oldDate;


Then you can use the properties on TimeSpan to get the relevant information

http://msdn.microsoft.com/en-us/library/system.timespan.aspx[^]

C#
public static int NumberOfWeeks(DateTime dateFrom, DateTime dateTo)
{
   TimeSpan Span = dateTo.Subtract(dateFrom);
   if (Span.Days <= 7)
   {
      if (dateFrom.DayOfWeek > dateTo.DayOfWeek)
      {
         return 2;
      }
      return 1;
   }
   int Days = Span.Days - 7 + (int)dateFrom.DayOfWeek;
   int WeekCount = 1;
   int DayCount = 0;
   for (WeekCount = 1; DayCount < Days; WeekCount++)
   {
      DayCount += 7;
   }
   return WeekCount;
}


That code was from http://madskristensen.net/post/Working-with-weeks-in-C-not-that-obvious.aspx[^]
 
Share this answer
 
Comments
gani7787 24-Jun-11 10:14am    
can you pls. provide vb.net code. so that it will be helpful for me...

Thanks..
Dylan Morley 24-Jun-11 10:15am    
No sorry, you can do that yourself - show a little initiative ;)
fjdiewornncalwe 24-Jun-11 10:55am    
Converting the code from C# into VB.NET is quite simple, especially for this example. Have a go at it and if you have issues relating to that translation, ask away and we'll be glad to help. Code translation like this is a great way for students to learn some of the ins and outs of a language. Do it and you will be amazed at how much a simple assignment like this can teach you.
thatraja 24-Jun-11 11:12am    
Convert the code yourself. Here you go. C#.NET to VB.NET
fjdiewornncalwe 24-Jun-11 10:53am    
Excellent answer. +5.
(I wish I could give you another +5 for the "you can do that yourself" comment). I appreciate it when the initiative is placed on the OP.
C#
public int NumberOfWeeks()
{
    int days = Math.Abs((d1.Date - d2.Date).Days) + 1;
    weeks = ((int)Math.Floor((decimal)days / 7));
    // if you want partial weeks added to it, add this line:
    weeks += (((decimal)days % 7) > 0) ? 1 : 0;
    return weeks;
}


EDIT ===============

Since you seem to be incabpable of converting simple code to VB, here it is:

VB
public Function NumberOfWeeks() As Integer
    Dim days As Integer = Math.Abs((d1.Date - d2.Date).Days) + 1
    Dim weeks As Integer = CType(Math.Floor((decimal)days / 7)), Integer)
    ' if you want partial weeks added to it, add this line:
    If ((decimal)days Mod 7) > 0) Then
       weeks += 1
    End If
    Return weeks
End Function
 
Share this answer
 
v4
Comments
RaviRanjanKr 24-Jun-11 14:55pm    
Nice Answer John, My vote of 5 :)
This need not be done in vb.net code. I think this calculation is best done at javascript. This can be done on a client side event based on your design.

C#
var date1 = new Date(parm1);
var date2 = new Date();
var weeks = (date2-date1)/(1000*60*60*24*7).toFixed(2);
 
Share this answer
 
You can use the class Week of the Time Period Library for .NET[^]:
C#
public int GetWeekOfYear( int year )
{
  return new Week( new DateTime( year, 12, 31 ) ).WeekOfYear;
} // GetWeekOfYear


Please note that Calendar.GetWeekOfYear does not calculate the week numbers according the ISO 8601 standard.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900