Working with the DateTime format for an international application

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

Based on my research:

  • Server Format: yyyy-mm-dd
  • Client Format: yyyy-mm-dd

Overwrite the binding of the DateTime model to ASP MVC with a custom mediator, for example

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 on the client side:

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

and one last question - setting above how the server checks for a DateTime Rejection or Rejection The client’s time zone if he must be a member of the account before going to the application?

+4
source share
2 answers

ISO string output is the right way.

You will probably find it helpful to use JavaScript Date toISOString . Since not every browser supports it, you want to provide 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'; }; }() ); } 

This is taken directly from MDN toISOString . I use it, and I hope most others do too.

Note that Z stands for Zulu time (GMT). You can simply use midnight ( T00:00:00.000Z ) so that there is no time. Personally, I tend not to care about the millisecond part for what I do, and omit it (the temporal resolution is reduced to the second).

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

Regarding the DateTime binding in MVC, you should parse the input value using the methods described in this answer . Key / date syntax analysis is a sequence, and as long as you can depend on the ISO format (either with T or with a space), you can easily control it.

+4
source

dateFormat(new Date(), 'Ym-dTH:i:s.uZ'); // Returns 2013-06-07T04:22:26.755

https://gist.github.com/poying/5942293

 var dateFormat = (function () { var keywords = { Y: 'getFullYear', m: 'getUTCMonth', d: 'getUTCDate', H: 'getUTCHours', i: 'getUTCMinutes', s: 'getUTCSeconds', u: 'getUTCMilliseconds' }; function pad(number) { var r = String(number); if ( r.length === 1 ) { r = '0' + r; } return r; } return function dateFormat(date, format) { var str = ''; var i, len = format.length; for (i = 0; i < len; i += 1) { if (keywords.hasOwnProperty(format[i])) { str += pad(Date.prototype[keywords[format[i]]].call(date)); } else { str += format[i]; } } return str; } })(); 
0
source

Source: https://habr.com/ru/post/1490097/


All Articles