【问题标题】:Assigning Value to Bootstrap-datetimepicker with Format MM/YYYY Displays Incorrect Year使用 MM/YYYY 格式为 Bootstrap-datetimepicker 分配值显示不正确的年份
【发布时间】:2018-06-05 18:54:16
【问题描述】:

我在 ASP MVC 4 应用程序中使用 bootstrap-datetimepicker 版本 4.17.47。

我有这个模型属性:

public DateTime MonthYear { get; set; }

我在控制器中分配默认值(任意值只是一个示例,实际值是通过一些逻辑确定的):

model.MonthYear = new DateTime(2017, 3, 1);

我使用 datepicker 在视图中显示此值,以便用户可以根据需要更新值:

@Html.TextBoxFor(model => model.MonthYear, new { @class = "form-control input month-picker" })

脚本:

$('.month-picker').datetimepicker({
    format: 'MM/YYYY',
    useCurrent: false,
    toolbarPlacement: 'bottom',
    viewMode: 'months'
});

问题是控件显示“03/0001”而不是“03/2017”。

我在这里错过了什么?

【问题讨论】:

  • 嗯.. 看起来确实没问题.. 您是否尝试删除 datetimepicker 只是为了查看 TextBox 是否填充了预期的数据?
  • @drigomed 当我删除选择器时我得到“3/1/2017 12:00:00 AM”

标签: asp.net-mvc-4 bootstrap-datetimepicker eonasdan-datetimepicker


【解决方案1】:

显然问题在于,当 datetimepicker 只期望 MM/YYYY 时,输入正在填充来自服务器的 DD/MM/YYYY 日期。尝试格式化文本框,例如:

@Html.TextBoxFor(model => model.MonthYear, "{0:MM/yyyy}", new { @class = "form-control input month-picker" })

应该可以的。

【讨论】:

    猜你喜欢
    • 2017-05-23
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    • 2019-10-29
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多