Created
May 3, 2017 07:22
-
-
Save jonasnordlund/53bcd43c7ea361d2de2f97f2f927202c to your computer and use it in GitHub Desktop.
Format ASP .NET web service dates.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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