简体   繁体   中英

Ignoring time zone in date object returned from an API using Angular

I'm getting a response from an API containing a date in the following format "/Date(1447773909000-0500)/"

I have an angular filter that pulls out the timestamp:

.filter('formattedDate', function () {
    return function (posteddate) {
        if (!posteddate || !posteddate.length) {
            return;
        }
        return posteddate.slice(6, 19);
    };
})

When I display the date I use two filters, mine and the angular date filter to properly format it.

<span class="list-content col-sm-6">{{appointment.ApptDateTime | formattedDate | date:'MMM dd, yyyy hh:mm a'}}</span>

The problem I'm having is the time is adjusting based off the users current time zone. I want to ignore this and instead use the value as it's returned with out altering it. I notice that depending on the timezone of the computer the response from the API changes the returned date. So instead of "/Date(1447773909000-0500)/" it will respond with "/Date(1447914211000-0700)/"

Try adding following line in your WebApiConfig file.

config.Formatters.JsonFormatter.SerializerSettings.DateTimeZoneHandling
= Newtonsoft.Json.DateTimeZoneHandling.Local;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM