0

I have a ASP.NET MVC 4 project with EF. In my (create) view I want to display beside an EditorFor for a date the week number of the current year.

I have a helper:

@model Helios.Models.tablename
@helper Week(DateTime dt)
{    
   CultureInfo ciCurr = CultureInfo.CurrentCulture;
   int weekNum = ciCurr.Calendar.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
   @weekNum
}
...
<div class="editor-field">
   @Html.TextBoxFor(model => model.date_birth, new {onchange="?"}) @Week(date_birth?)
</div>

I'm not sure if this can be acomplished in Razor, but I need to update the week number if I change the date

enter image description here

Q : How can I display the week no. beside the datepicker and update it if the date is changed ?

Community
  • 1
  • 1
Misi
  • 748
  • 5
  • 21
  • 46

1 Answers1

2

Here is a post on that deals with calculating the week number.

How can I calculate/find the week-number of a given date?

But it all depends on if you plan to set it on the server or if the client is to be able to change date. If the client can change the date then you would need javascript instead.

function(d) {

 var day = d.getDay();
 if(day == 0) day = 7;
 d.setDate(d.getDate() + (4 - day));
 var year = d.getFullYear();
 var ZBDoCY = Math.floor((d.getTime() - new Date(year, 0, 1, -6)) / 86400000);
 return 1 + Math.floor(ZBDoCY / 7);
}

From: http://jquery142.blogspot.se/2010/04/how-to-get-week-number-for-date-in.html

Community
  • 1
  • 1
Patrick
  • 5,442
  • 9
  • 53
  • 104