2012-07-12 74 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