Tutorial :Date range from weeknumber + year



Question:

I have 2 dropdowns 1 with weeknumbers 1 with years. I want to extract the date range from this data.

So:

Weeknumber 13 year 2009 would give:

monday 23 march 2009 tuesday 24 march 2009 ...

VB.Net preferred but C# sollution is also ok.

Edit: Ok I guess I should have mentioned this is for European dates.


Solution:1

var date = DateTime.MinValue + 2009.Years() + 13.Weeks();  

by using Fluent DateTime project on Codeplex.


Solution:2

CultureInfo curCulture = CultureInfo.CurrentCulture;    DateTime targetDate = curCulture.Calendar.AddWeeks(new DateTime([year], 1, 1), [Week]);    DayOfWeek targetWeekDay =      curCulture.Calendar.GetDayOfWeek(targetDate);    DateTime targetBeginningOfWeek = targetDate.AddDays(-1*Convert.ToInt16(targetWeekDay));  

targetBeginningOfWeek will contain first day of that week, add 7 days and get rest of day in that week


Solution:3

Try to use these following functions:

Public Function Week2Date1(ByVal Week2Date2 As Date) As Date      Week2Date1 = DateAdd(DateInterval.Day, -4, Week2Date2)    End Function    Public Function Week2Date2(ByVal WeekNo As Integer) As Date      Week2Date2 = DateSerial(Now.Year, 1, (WeekNo) * 7)    End Function  

I am using these to determine the date of the Mondays and Fridays of every week number.

where:

  • on Function Week2Date2 will return the Friday of the Week Number and
  • on Function Week2Date1 will return the Monday of the Week Number from the date value returned by Week2Date2.


Solution:4

Below code gets the date range from week number and year. But its written in Java.
Hope it helps.

   System.out.println("date Range from weekNumber and year but in Java");     System.out.println(); // print a blank line       // get the input from the user     Scanner sc = new Scanner(System.in);       System.out.print("Enter the week : ");     int weekNumber  = sc.nextInt();      System.out.print("Enter the Year: ");     int year = sc.nextInt() ;           Calendar cal = Calendar.getInstance();     //cal.setTime(new Date());       cal.set(Calendar.YEAR, year);     cal.set(Calendar.WEEK_OF_YEAR, weekNumber);       SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");       cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);     System.out.println(formatter.format(cal.getTime())); // start date       cal.add(Calendar.DAY_OF_WEEK, 6);     System.out.println(formatter.format(cal.getTime())); // end date  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »