/* The MIT License (MIT) Copyright (c) 2014 Andrew Davey ([email protected]) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ // Define our application module. var app = angular.module("app", []); // Configure the $httpProvider by adding our date transformer app.config(["$httpProvider", function ($httpProvider) { $httpProvider.defaults.transformResponse.push(function(responseData){ convertDateStringsToDates(responseData); return responseData; }); }]); var regexIso8601 = /^(\d{4}|\+\d{6})(?:-(\d{2})(?:-(\d{2})(?:T(\d{2}):(\d{2}):(\d{2})\.(\d{1,})(Z|([\-+])(\d{2}):(\d{2}))?)?)?)?$/; function convertDateStringsToDates(input) { // Ignore things that aren't objects. if (typeof input !== "object") return input; for (var key in input) { if (!input.hasOwnProperty(key)) continue; var value = input[key]; var match; // Check for string properties which look like dates. // TODO: Improve this regex to better match ISO 8601 date strings. if (typeof value === "string" && (match = value.match(regexIso8601))) { // Assume that Date.parse can parse ISO 8601 strings, or has been shimmed in older browsers to do so. var milliseconds = Date.parse(match[0]); if (!isNaN(milliseconds)) { input[key] = new Date(milliseconds); } } else if (typeof value === "object") { // Recurse into object convertDateStringsToDates(value); } } } app.controller("DemoController", ["$http", "$scope", DemoController]); function DemoController($http, $scope) { $scope.form = { date: "1984-09-25" }; $scope.go = function () { var request = queryServer($http, $scope.form); request.then(function (response) { $scope.isDate = response.data.date instanceof Date; $scope.date = response.data.date; }); }; $scope.go(); } // Use JSFiddle's echo service to return back our test data. function queryServer($http, responseData) { var json = encodeURIComponent(angular.toJson(responseData)); var request = $http.post( '/echo/json/', 'json=' + json, { headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' } }); return request; }
<div ng-app="app" ng-controller="DemoController"> <form ng-submit="go()"> <label for="date-input">Date string returned from server</label> <input type="text" id="date-input" ng-model="form.date" value="1984-09-25" /> <button type="submit">Go</button> </form> <div ng-show="date"> <p><code>response.data.date instanceof Date === {{isDate}}</code> </p> <p ng-show="isDate">Display using Angular's date filter: {{date | date:'longDate'}}</p> </div> </div>