Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with DateTime format for international application

What is the preferred practice to handle 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 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?

like image 710
JeeShen Lee Avatar asked Jul 07 '13 03:07

JeeShen Lee


People also ask

What is the standard international date format?

The international format yyyy-mm-dd or yyyymmdd is also accepted, though this format is not commonly used. The formats d. 'month name' yyyy and in handwriting d/m-yy or d/m yyyy are also acceptable.)

How do you write time in international system?

Times with AM, PM, MN and NN As an internationally understandable format, it is becoming more and more common to use the 24-hour format "05:30." It is important here to write the hour with two digits, i.e., with a leading zero. This turns the time into the early morning, at half past five.

How do you format datetime?

For example, the "d" standard format string indicates that a date and time value is to be displayed using a short date pattern. For the invariant culture, this pattern is "MM/dd/yyyy". For the fr-FR culture, it is "dd/MM/yyyy". For the ja-JP culture, it is "yyyy/MM/dd".

What countries use mm dd yyyy?

Basic group behaviour shows it's weird. Despite the variety of date formats used around world, the US is the only country to insist on using mm-dd-yyyy.


1 Answers

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

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';
    };

  }() );
}

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

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).

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

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.

like image 185
pickypg Avatar answered Sep 30 '22 13:09

pickypg