Skip to content

Instantly share code, notes, and snippets.

@jonasnordlund
Created May 3, 2017 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonasnordlund/53bcd43c7ea361d2de2f97f2f927202c to your computer and use it in GitHub Desktop.
Save jonasnordlund/53bcd43c7ea361d2de2f97f2f927202c to your computer and use it in GitHub Desktop.
Format ASP .NET web service dates.
/**
* @description Although Microsoft have improved this situation with Web API,
ASP .NET applications still return JSON dates in a proprietary
format similar to "/Date(1320825600000-0800)/". This function
parses these dates into a user locale depending date string.
* @param {string} s The ASP .NET date to parse and format.
* @returns {string} The formatted date.
*/
aspNetDate: function (s) {
var dateParts = s.match(/\/Date\((\d+)[+-](\d+)\)\//);
if (!dateParts)
return s; // Unexpected format.
var unixEpoch = parseInt(dateParts[1]);
var tzInSeconds = (dateParts[2] / 100) * 60 * 60;
return new Date(unixEpoch + tzInSeconds).toLocaleString();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment