与DateTime格式为国际申请处理 [英] Dealing with DateTime format for international application

查看:108
本文介绍了与DateTime格式为国际申请处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是preferred的做法来处理的 DateTime格式客户端(JavaScript中,AJAX)和服务器(ASP MVC)的国际申请?

What is the preferred practice to handle DateTime format between client (javascript, ajax) and server (ASP MVC) for an international application?

根据我的研究:


  • 服务器格式:YYYY-MM-DD

  • 客户端格式:YYYY-MM-DD

覆盖自定义模型绑定ASP MVC的日期时间模型绑定像

Overwrite the DateTime model binder of ASP MVC with custom model binder like

    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        try
        {
            var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
            return value.ConvertTo(typeof(DateTime), CultureInfo.InvariantCulture);
        }
        catch (Exception ex)
        {
            return new DateTime();
        }
    }

,并通过在客户端格式化日期:

and format the date at client side by:

    function toISOString(d) {
        var year = d.getFullYear();
        var month = d.getMonth() + 1;
        var date = d.getDate();
        return year + '-' + month + '-' + date;
    }

和最后一个问题 - 已经设置以上,服务器如何检查日期时间偏移时区偏移的客户端如果有采取之前去交代到应用程序?

and one last question - having set the above, how the server check the DateTime offset or Timezone offset of the client if that has to take in to account before going into the application?

推荐答案

1.4.3作为ISO字符串是正确的道路要走。

Outputting as an ISO string is the right way to go.

这可能有利于你使用JavaScript 的日期 toISOString 。由于不是每个浏览器支持它,你将要提供它不这样做的浏览器:

It will probably benefit you to use the JavaScript Date's toISOString. As not every browser supports it, you will want to supply it for browsers that do not:

if ( !Date.prototype.toISOString ) {
  ( function() {

    function pad(number) {
      var r = String(number);
      if ( r.length === 1 ) {
        r = '0' + r;
      }
      return r;
    }

    Date.prototype.toISOString = function() {
      return this.getUTCFullYear()
        + '-' + pad( this.getUTCMonth() + 1 )
        + '-' + pad( this.getUTCDate() )
        + 'T' + pad( this.getUTCHours() )
        + ':' + pad( this.getUTCMinutes() )
        + ':' + pad( this.getUTCSeconds() )
        + '.' + String( (this.getUTCMilliseconds()/1000).toFixed(3) ).slice( 2, 5 )
        + 'Z';
    };

  }() );
}

这是直接从<一个取href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString\"相对=nofollow> MDN toISOString 。我用它,我希望大多数人也有同感。

That is taken directly from MDN toISOString. I use it, and I hope that most others are too.

请注意在以Z 表示祖鲁时间(GMT)。你可以只用午夜( T00:00:00.000Z )表示没有时间。就个人而言,我倾向于不关心毫秒部分为我做什么,我忽略它(时间分辨率精细精确到秒)。

Note the Z stands for Zulu time (GMT). You can just use midnight (T00:00:00.000Z) to denote no time. Personally, I tend not to care about the milliseconds portion for what I do and I omit it (time resolution is fine down to the second).

只要你的ISO格式规范,那么你可以随便写,如果必要的服务器和客户端简单的解析器。

As long as you standardize on the ISO format, then you can easily write simple parsers for both the server and client, if necessary.

对于的DateTime 在MVC约束力,应使用方法然后解析传入的值的在这个答案描述。日期/时间分析的关键是一致性好,只要你可以依靠ISO格式(与 T 或使用空格),那么你就可以轻松管理吧。

As for the DateTime binding in MVC, you should then parse the incoming value using the methods described in this answer. The key to date/time parsing is consistency, and as long as you can depend on the ISO format (either with the T or using a space), then you can easily manage it.

这篇关于与DateTime格式为国际申请处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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