asp.net mvc-设置日期时间参数的语言环境/格式 [英] asp.net mvc - Set locale / format of a DateTime parameter

查看:190
本文介绍了asp.net mvc-设置日期时间参数的语言环境/格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用bootrap日期时间选择器来允许用户输入日期,如下所示:

I am using the bootrap datetime picker to allow the user to enter a date, show below:

<div class="form-group">
    <label for="end">Start date:</label><br />
    <div class='input-group date' id='dt1'>
        @Html.TextBox("StartDateFilter", null, new { @class = "form-control" })
        <span class="input-group-addon">
            <span class="glyphicon glyphicon-calendar"></span>
        </span>
    </div>
</div>

<script type="text/javascript">
    $('#dt1').datetimepicker({
        format: 'L',
        locale: 'en-GB'
    });
</script>

然后在以下方法签名中将此日期作为参数:

This date then gets picked up as a parameter in the following method signature:

public ActionResult Index(string sortOrder, string sortBy, string currentFilter, string searchString, string filterType, int? itemDisplay, int? page, DateTime? startDateFilter, DateTime? endDateFilter)

但是,文本框中的日期显示为DD / MM / YYYY但已通过格式为MM / DD / YYYY,这会导致一些格式错误和解析错误。如何将参数中的DateTime对象的格式或本地更改为DD / MM / YYYY。

However, the date in the textbox is being displayed as DD/MM/YYYY but passed as MM/DD/YYYY which causes several formatting errors and parsing errors. How can I change the format or local of the DateTime objects that are in the parameters to DD/MM/YYYY.

我已经在我的web.config中添加了以下内容system.web下的文件:

I have already added the following to my web.config file under system.web:

<globalization culture="en-GB" uiCulture="en-GB" />

预先感谢。

推荐答案

您可以使用此

更新

global.asax

global.asax

    protected void Application_Start()
    {
        GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings =
          new JsonSerializerSettings
      {
         DateFormatHandling = DateFormatHandling.IsoDateFormat,
         DateTimeZoneHandling = DateTimeZoneHandling.Unspecified,
         Culture = CultureInfo.GetCultureInfo("en-GB")
      };

         Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
    }

这篇关于asp.net mvc-设置日期时间参数的语言环境/格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆