2012-07-12 75 views
0

我有一个与MV的ASP.NET MVC 4项目。在我的(创建)视图中,我想在EditorFor旁边显示当前年份的星期数。ASP.NET MVC计算周

我有一个帮手:

@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> 

我不知道这是否可以在剃刀acomplished,但我需要,如果我更改日期更新周数

enter image description here

问题:我如何显示第一周。在日期选择器旁边并且如果日期改变就更新它?

+0

你使用代码更改日期或在于用户可以更改日期的想法? – Patrick 2012-07-12 07:54:22

+0

用户从TextBoxFor控件更改日期 – Misi 2012-07-12 21:26:52

回答

2

这是一篇关于计算周数的文章。

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

但是,如果您计划设置在服务器上或者客户端是能够改变迄今为止这一切都取决于。如果客户可以更改日期,那么您需要JavaScript来代替。

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); 
} 

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