Here are the following methods to get Year Number
along with Week number from two date difference.
We are tried to use two ways to get the result using
Array List and Generic List. I hope you will get some helpful information about
ArrayList conversion (from arraylist to variable) and Generic List conversion.
Let me know your feedback and suggestions.
List<WeekYear> myWeek = new
List<WeekYear>(); //declaration above page load
protected void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
//get year & week number using Array List
ArrayList
arryWeek = GetWeekNumberYearByArrayList();
for
(int i=0; i
{
int weekNo = Convert.ToInt32(((string[])(arryWeek[i]))[0]); //week no.
int
yearNo = Convert.ToInt32(((string[])(arryWeek[i]))[1]); //year
no.
Response.Write("Week " + weekNo + " Year " + yearNo + "
");
");
}
//get year & week number using Generic List
myWeek = GetWeekNumberYear();
for
(int i = 0; i < myWeek.Count; i++)
{
int
weekNo = Convert.ToInt32(myWeek[i].wkNumber);
int
yearNo = Convert.ToInt32(myWeek[i].yrNumber);
Response.Write("Week " + weekNo + "Year " + yearNo + "
");
");
}
}
}
//using Array List
protected
ArrayList GetYearWeekByArrayList()
{
DateTime
dt1 = new DateTime();
DateTime
dt2 = new DateTime();
dt1 = Convert.ToDateTime("08/04/2012"); //Date
format in dd/mm/yyyy
dt2 = Convert.ToDateTime("08/04/2014"); //Date
format in dd/mm/yyyy
var
result = new ArrayList();
for (DateTime dtWeek = dt1; dtWeek <= dt2; dtWeek =
dtWeek.AddDays(7))
{
String[]
arr = new String[2];
int
WeekNo = (int)((dtWeek - new DateTime(dt1.Year,
dt1.Month, 1)).TotalDays / 7);
int
yearNo = dtWeek.Year;
arr[0] = WeekNo.ToString();
arr[1] = yearNo.ToString();
result.Add(arr);
}
return
result;
}
//using Generic List
protected List<WeekYear> GetWeekNumberYear()
{
DateTime
dt1 = new DateTime();
DateTime
dt2 = new DateTime();
dt1 = Convert.ToDateTime("08/04/2012");
dt2 = Convert.ToDateTime("08/04/2014");
for (DateTime dtWeek = dt1; dtWeek <= dt2;
dtWeek=dtWeek.AddDays(7))
{
int
WeekNo = (int)((dtWeek - new DateTime(dt1.Year,
dt1.Month, 1)).TotalDays/7);
int
yearNo = dtWeek.Year;
myWeek.Add(new WeekYear((int)WeekNo, (int)yearNo));
}
return
myWeek;
}
//Generic list class
declaration
public class
WeekYear
{
public
WeekYear(int weekNumber, int yearNumber)
{
this.wkNumber
= weekNumber;
this.yrNumber
= yearNumber;
}
public
int wkNumber;
public int yrNumber;
}
Thanks & Regards,
Ved Pathak
Comments