[英]Parse String (yyyymmm) Format to DateTime?
I have a field called Accounting Period as char, [AccountingPeriod] [char](6)
, how can I parse it to DateTime Format using DatePicker?我有一个名为 Account Period as char 的字段
[AccountingPeriod] [char](6)
,如何使用 DatePicker 将其解析为 DateTime 格式?
I used this KendoGrid for DatePicker我将这个 KendoGrid 用于 DatePicker
@(
Html.Kendo().DatePicker()
.Name(nameof(InvoiceDTO.AccountingPeriod))
.Format("{0:yyyyMM}")
.HtmlAttributes(new { @class = "form-control", style = "width:50%" })
)
I have the date: 199805
我有日期:
199805
Okay, as from the comments I have understood that you have a date in "yyyyMM" format and from that you want the magical line of code to produce you an Object of type DateTime
.好的,从评论中我了解到您有一个“yyyyMM”格式的日期,并且您希望神奇的代码行为您生成一个
DateTime
类型的 Object 。 You can do it using this line of code:您可以使用这行代码来做到这一点:
DateTime.TryParseExact("199805", "yyyyMM", CultureInfo.InvariantCulture, DateTimeStyles.None, out var theDate);
Feel free to modify it according to your needs.随意根据您的需要进行修改。
UPDATE:更新:
Regarding your assignment of the date to the Kendo
datepicker, I would suggest you to create a method and call it inside of the Value()
method.关于您将日期分配给
Kendo
日期选择器,我建议您创建一个方法并在Value()
方法中调用它。 Something similar to this:与此类似的东西:
@functions{
public DateTime GetDate(string strDate)
{
if (DateTime.TryParseExact(strDate, "yyyyMM", CultureInfo.InvariantCulture, DateTimeStyles.None, out var theDate))
{
return theDate;
}
return DateTime.Now;
}
}
And then you can just call it inside the Value()
method something like below:然后你可以在
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: I have not tested the code, just fire in the air after going through this article . PS:我没有测试过代码,看完这篇文章就开枪了。 So, It might need some Love.
所以,它可能需要一些爱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.