如何获得Kendo UI MVC datetimepicker,以在URL/表单中明确地格式化日期? [英] How can I get the Kendo UI MVC datetimepicker to format the date unambiguously in the URL/form?
问题描述
我正在尝试使用Kendo UI MVC dateTimePicker明确格式化日期,以便MVC模型绑定能够理解它.我有一个datetimepicker控件,该控件的形式是通过GET请求提交的.
I'm trying to use the Kendo UI MVC dateTimePicker to format the date unambiguously so that MVC model binding understands it. I have a datetimepicker control in a form which submits via a GET request.
MVC希望使用以下格式的日期:yyyy-MM-dd,但我想在页面上将日期显示为dd/MM/yyyy
MVC expects dates in this format: yyyy-MM-dd but I want to display dates on the page as dd/MM/yyyy
如果我使用此代码,则MVC会正确解析日期,但会首先向用户显示年份,这不是我想要的:
If I use this code, MVC parses the date correctly, but displays the year first to the user, which isn't what I want:
@(Html.Kendo().DateTimePicker()
.Name("ToDate")
.Format("yyyy-MM-dd")
)
有没有解决方法,所以我可以在框中设置人类的日期格式,但在URL中使用明确的(不变的)格式?
Is there a workaround so I can format the date for humans in the box, but format it in an unambiguous (invariant) format in the URL?
推荐答案
您应将DateTimePicker格式配置为用户友好格式.然后使用javascript从小部件获取Date对象(而不是字符串date).然后将此日期对象格式化为"yyyy-mm-dd"并发出请求.
You should configure your DateTimePicker format to a user friendly format. Then use javascript to get the Date object from the widget (not string date). Then format this date object to 'yyyy-mm-dd' and make the request.
因此您的小部件应该是:
So your widget shoul be:
@(Html.Kendo().DateTimePicker()
.Name("ToDate")
.Format("dd/MM/yyyy")
)
然后使用此javascript发出请求:
Then use this javascript to make the request:
var date = $("#ToDate").data("kendoDateTimePicker").value();
date变量具有可以设置格式的javascript日期:
date variable have a javascript date you can format:
function dateToString(date){
var str = date.getFullYear() + "-";
str += ('0' + (date.getMonth() + 1)).slice(-2) + "-";
str += ('0' + date.getDate()).slice(-2) + "T";
str += ('0' + date.getHours()).slice(-2) + "-";
str += ('0' + date.getMinutes()).slice(-2) + "-";
str += ('0' + date.getSeconds()).slice(-2);
return str;
}
此格式为"yyyy-mm-dd hh:mm:ss",但是您可以根据需要删除时间部分
This format is "yyyy-mm-dd hh:mm:ss", but you can remove the time part if you want
这篇关于如何获得Kendo UI MVC datetimepicker,以在URL/表单中明确地格式化日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!