[英]Parse String (yyyymmm) Format to DateTime?
我有一个名为 Account Period as char 的字段[AccountingPeriod] [char](6)
,如何使用 DatePicker 将其解析为 DateTime 格式?
我将这个 KendoGrid 用于 DatePicker
@(
Html.Kendo().DatePicker()
.Name(nameof(InvoiceDTO.AccountingPeriod))
.Format("{0:yyyyMM}")
.HtmlAttributes(new { @class = "form-control", style = "width:50%" })
)
我有日期: 199805
好的,从评论中我了解到您有一个“yyyyMM”格式的日期,并且您希望神奇的代码行为您生成一个DateTime
类型的 Object 。 您可以使用这行代码来做到这一点:
DateTime.TryParseExact("199805", "yyyyMM", CultureInfo.InvariantCulture, DateTimeStyles.None, out var theDate);
随意根据您的需要进行修改。
更新:
关于您将日期分配给Kendo
日期选择器,我建议您创建一个方法并在Value()
方法中调用它。 与此类似的东西:
@functions{
public DateTime GetDate(string strDate)
{
if (DateTime.TryParseExact(strDate, "yyyyMM", CultureInfo.InvariantCulture, DateTimeStyles.None, out var theDate))
{
return theDate;
}
return DateTime.Now;
}
}
然后你可以在Value()
方法中调用它,如下所示:
@(
Html.Kendo().DatePicker()
.Name(nameof(InvoiceDTO.AccountingPeriod))
.Value(GetDate(InvoiceDTO.AccountingPeriod)) // I assume AccountPeriod is of type string.
// .Format("{0:yyyyMM}") I do not think we are going to need this after the date is parsed into the required type.
.HtmlAttributes(new { @class = "form-control", style = "width:50%" })
)
PS:我没有测试过代码,看完这篇文章就开枪了。 所以,它可能需要一些爱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.