我试图将我的日期输入最大属性限制为流动的,以便它每年都会发生变化。不像现在那样硬编码。
现在:<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");
}
每次尝试都不成功,我可以选择此范围之外的日期。也许与更改格式有关?我该怎么做?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
整个事情可以压缩为 1 行代码:
public string MaxRetiredDate = $"{(DateTime.Today.AddYears(1)).Year}-12-31";