angularjs - How to convert date from UTC to local time zone and reverse with Restangular -
i storing date in utc format on node/express server , using restangular , post data on client. possible convert date utc local time , local time utc restangular addresponseinterceptor , addrequestinterceptor methods.
this post solution me. works great - change made add tolocal flag, set true on response interceptor, , false on request, used moment appropriate switch..
http://aboutcode.net/2013/07/27/json-date-parsing-angularjs.html
function convertdatestringstodates(input, tolocal) { // ignore things aren't objects. if (typeof input !== "object") return input; (var key in input) { if (!input.hasownproperty(key)) continue; var value = input[key]; var match; // check string properties dates. if (typeof value === "string" && (match = value.match(regexiso8601))) { var milliseconds = date.parse(match[0]); if (!isnan(milliseconds)) { if (tolocal) { input[key] = moment.utc(new date(milliseconds)).local().todate(); } else { input[key] = moment(new date(milliseconds)).utc().todate(); } } } else if (typeof value === "object") { // recurse object convertdatestringstodates(value, tolocal); } } return input;
}
then...
.config([ '$httpprovider', function($httpprovider) { $httpprovider.interceptors.push(function () { return { request: function(request) { return convertdatestringstodates(request,false); }, response: function (response) { // otherwise, default behaviour return convertdatestringstodates(response, true); } }; }); } ])
Comments
Post a Comment