The time returned from the REST API is wrong

This article covers a scenario where developers are consuming the REST API using C# server-side integration.  This article is not as relevant to you if you're using JavaScript to consume the API.

Problem

When you build an integration with the REST API, the time you see returned by the web service doesn't match the actual time you see in the administration area, or the UTC version of that time in the database.

As an example, a customer may place an order at 11:49 AM EDT, and you'll see this accurately reflected in the order details.

Next, you can check the database and see that the date is accurate too, but converted to UTC.

However, the API shows you a different time altogether.  Note that in this case, the time appears to be off by an hour, but it's also PM, when it should be AM.  Depending on the time of day the order was placed, this could appear to be even worse - in another day.

Cause

This is actually a very common and expected issue when consuming ASP.NET web services, especially when using C# to do it.  The issue is a known and accepted one, unfortunately, where the ASP.NET date serializers don't serialize the date the way you'd expect.  As a result, you'll have another step or two in your own application.

Resolution

Consider the following code example.  This code was used to generate the screenshot above.

// create the required API values
var url = "http://mydomain.local";
var key = "1-965a1685-af02-4f29-be57-aa1f4a9d9681";

// get an API object
var proxy = new Api(url, key);

// specify the order to retrieve
var orderBvin = "4f3b48af-53d7-4ef6-b443-ec9fcddd236f";

// get the order from the store API
var orderObject = proxy.OrdersFind(orderBvin);

// no conversion yet
Console.WriteLine("Order {0} placed: {1}", orderObject.Content.OrderNumber, orderObject.Content.TimeOfOrderUtc);
Console.WriteLine();

The time stamp was simply returned to the view.  With a simple change, you'll have the date and time you were expecting.

In the code sample below, you'll see that the date and time first get converted from UTC using the built-in TimeZoneInfo.ConvertTimeFromUtc() method.  Then, you simply call the .ToLocalTime() property from the DateTime object to get the expected result.

// A 'hack' for the code below to get a timezone
// you can do this any number of ways
var timeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");

// convert the value to get it's UTC equivalent
var timeStamp = TimeZoneInfo.ConvertTimeFromUtc(orderObject.Content.TimeOfOrderUtc, timeZone);

// this is the value you were looking for
var convertedTimeStamp = timeStamp.ToLocalTime();

// show the converted time stamp
Console.WriteLine("Converted order {0} timestamp: {1}", orderObject.Content.OrderNumber, convertedTimeStamp);
Console.WriteLine();

What you end up with is the time you expected originally.  This date and time matches what you see in the order now.

Here's the full code sample, for your convenience.

// create the required API values
var url = "http://mydomain.local";
var key = "1-965a1685-af02-4f29-be57-aa1f4a9d9681";

// get an API object
var proxy = new Api(url, key);

// specify the order to retrieve
var orderBvin = "4f3b48af-53d7-4ef6-b443-ec9fcddd236f";

// get the order from the store API
var orderObject = proxy.OrdersFind(orderBvin);

// no conversion yet
Console.WriteLine("Order {0} placed: {1}", orderObject.Content.OrderNumber, orderObject.Content.TimeOfOrderUtc);
Console.WriteLine();

// A 'hack' for the code below to get a timezone
// you can do this any number of ways
var timeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");

// convert the value to get it's UTC equivalent
var timeStamp = TimeZoneInfo.ConvertTimeFromUtc(orderObject.Content.TimeOfOrderUtc, timeZone);

// this is the value you were looking for
var convertedTimeStamp = timeStamp.ToLocalTime();

// show the converted time stamp
Console.WriteLine("Converted order {0} timestamp: {1}", orderObject.Content.OrderNumber, convertedTimeStamp);
Console.WriteLine();
Have more questions? Submit a request

Need More Help?

Do you need more assistance with this article? Please review your support options.