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.

KyleAT
  • 71
  • 1
  • 8
  • is this of help: [JSON Date and DateTime serialisation in c# & newtonsoft](https://stackoverflow.com/questions/46120116/json-date-and-datetime-serialisation-in-c-sharp-newtonsoft) ? – Luuk Jan 22 '21 at 07: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. – Fildor Jan 22 '21 at 07:43
  • 1
    Also see: [DateTimeOffset.FromUnixTimeMilliseconds(Int64) Method](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.fromunixtimemilliseconds?view=net-5.0) – Fildor Jan 22 '21 at 07: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. – KyleAT Jan 22 '21 at 08:12

1 Answers1

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

Kirjava
  • 226
  • 1
  • 11
  • Thank you for the answer, however, I'm getting an error when adding, it says: "Cannot convert from 'string' to 'double' – KyleAT Jan 22 '21 at 08:03
  • 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 – Kirjava Jan 22 '21 at 08:04
  • 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 – KyleAT Jan 22 '21 at 08:11
  • Your value is not seconds but milliseconds. I'd also recommend to use `FromUnixTimeMilliseconds`. – Fildor Jan 22 '21 at 08:15
  • 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. – KyleAT Jan 22 '21 at 08:23
  • Why? To make it human-readable in the DB? – Fildor Jan 22 '21 at 08:30
  • @Fildor Yes, instead of it being just a large set of numbers, I wanted it to be formatted into DateTime. – KyleAT Jan 22 '21 at 08:31
  • Honestly, I'd make a view for that but keep the original numerical unixtimemillis. – Fildor Jan 22 '21 at 08:33
  • @Fildor Ah okay so I don't have to convert it? Do you have a link on how to do that by the way? To visually convert it so it's just there on view? – KyleAT Jan 22 '21 at 08:36
  • Maybe have a look at this: https://learn.microsoft.com/en-us/sql/relational-databases/views/views?view=sql-server-ver15 – Fildor Jan 22 '21 at 08:40
  • Can you give us the Create Table script in order to check the column type ? If you are using SQL Server, in addition to the views link, here are the dates representations you can get from a DateTime format : https://learn.microsoft.com/en-us/sql/t-sql/data-types/datetime-transact-sql?view=sql-server-ver15 I will update the script above to be compliant with the Unix format – Kirjava Jan 22 '21 at 08:50