我試圖將我的日期輸入最大屬性限制為流動的,以便它每年都會發生變化。不像現在那樣硬編碼。
現在:<input type="date" @bind-value="product.DateRetired" min="1950-01-01" max="2024-12-31" />
我嘗試將其分解為:
嘗試1:
<input type="date" @bind-value="product.DateRetired" min="1950-01-01" max="@GetNextYear-12-31" /> private int GetNextYear() { DateTime thisyearaddone = DateTime.Today.AddYears(1); int nextyear = thisyearaddone.Year; return nextyear; }
嘗試2:
<input type="date" @bind-value="product.DateRetired" min="1950-01-01" max="@GetNextYearDate" /> private DateTime GetNextYear() { DateTime thisyearaddone = DateTime.Today.AddYears(1); int nextyear = thisyearaddone.Year; DateTime maxretireddate = new DateTime(nextyear, 12, 31); return maxretireddate; }
嘗試3:
public string MaxRetiredDate; <input type="date" @bind-value="product.DateRetired" min="1950-01-01" max="@MaxRetiredDate" onclick="@GetMaxRetiredDate" /> private void GetMaxRetiredDate() { DateTime NextYearDate = DateTime.Today.AddYears(1); int NextYearInt = NextYearDate.Year; DateTime MaxRetiredDate = new DateTime(NextYearInt, 12, 31); MaxRetiredDate.ToString("yyyy-mm-dd"); }
每次嘗試都不成功,我可以選擇此範圍之外的日期。也許與更改格式有關?我該怎麼做?
整件事情可以壓縮為 1 行程式碼: