1

I'm currently building a project that retrieves API data and saves it into a database. Everything is working fine except for the DateTime values in the API. I have a class that uses RestSharp to obtain the API data then it uses NewtonSoft.Json to derserialize the API data into a JSON format which is then stored into a temporary DTO class file. Here is the API method.

public static void getAllRequestData()
{
    var client = new RestClient("[My API URL]");
    var request = new RestRequest();
    var response = client.Execute(request);

    if (response.StatusCode == System.Net.HttpStatusCode.OK)
    {
        string rawResponse = response.Content;
        AllRequests.Rootobject result = JsonConvert.DeserializeObject<AllRequests.Rootobject>(rawResponse);
    }
} 

Now here is the DTO file (AllRequests) that will temporarily store the Converted JSON data.

public class AllRequests
    {
        public class Rootobject
        {
            public Operation Operation { get; set; }
        }

        public class Operation
        {
            public Result Result { get; set; }
            public Detail[] Details { get; set; }
        }

        public class Result
        {
            public string Message { get; set; }
            public string Status { get; set; }
        }

        public class Detail
        {
            [Key]
            public int Id { get; set; }
            public string Requester { get; set; }
            public string WorkOrderId { get; set; }
            public string AccountName { get; set; }
            public string CreatedBy { get; set; }
            public string Subject { get; set; }
            public string Technician { get; set; }
            public string IsOverDue { get; set; }
            public string DueByTime { get; set; }
            public string Priority { get; set; }
            public string CreatedTime { get; set; }
            public string IgnoreRequest { get; set; }
            public string Status { get; set; }
        }
    }

The lines of code in Details that I want to be DateTime formats are "DueByTime" and "CreatedTime" instead of being String values. Currently they're only holding JSON format DateTime values in a String such as "1477394860065".

I've tried making "public string CreatedTime { get; set; }" to "public DateTime CreatedTime { get; set; }" However that only returned an error since it's JSON format. How could I rectify this issue so that it's stored in the DTO correctly in a DateTime format? Because ideally I want to scaffold this class file into a table so it can hold data in a database.

For more context to this, here's what I want rectified in my Database.

enter image description here

I want there to be a DateTime shown instead of a long list of numbers like there is here under Createby and DueBy.

Any help would be appreciated.

4
  • is this of help: JSON Date and DateTime serialisation in c# & newtonsoft ? Commented Jan 22, 2021 at 7:40
  • 2
    It's a unix timestamp in millis. You can have that deserialized into a long and then have a readonly jsonignore property where in the getter you convert that to DateTime for starters. Commented Jan 22, 2021 at 7:43
  • 1
    Also see: DateTimeOffset.FromUnixTimeMilliseconds(Int64) Method Commented Jan 22, 2021 at 7:50
  • Thank you for the answers, @Fildor I've tried that implementation that someone posted below. However, it's still not converting into a DateTime when I scaffold the database and run the application. Commented Jan 22, 2021 at 8:12

1 Answer 1

1

[EDIT] added the unix time format compliance[/EDIT]

Just putting in code what @Fildor said

public long CreatedTime { get; set; }

[JsonIgnore] // will ignore the property below when serializing/deserializing
public DateTimeOffset CreatedTimeDate { 
    // Don't need a setter as the value is directly get from CreatedTime property
    get {
        return DateTimeOffset.FromUnixTimeMilliseconds(CreatedTime);
    }
}

Used also this answer to convert to DateTime as asked, using the local time.

Here is how you convert to DateTime if you don't need the offset : https://learn.microsoft.com/fr-fr/dotnet/standard/datetime/converting-between-datetime-and-offset

Sign up to request clarification or add additional context in comments.

11 Comments

Thank you for the answer, however, I'm getting an error when adding, it says: "Cannot convert from 'string' to 'double'
mistake I mad. I correct it right now. I kept the type string instead of long or double. So the DateTime conversion failed as it expected a double
I've just tried it now, no errors but there's still no conversion for some reason. When I scaffold the database and pull the API data, it's still set in a number format rather than a DateTime, very strange
Your value is not seconds but milliseconds. I'd also recommend to use FromUnixTimeMilliseconds.
I have updated my question and you can see an image of the table that I'm talking about. I want it to be in a DateTime format instead of the long list of numbers that it is now. That return value doesn't appear to be returning for some reason.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.