Home > Software engineering >  How to get a datetime format like "00:00:00"?
How to get a datetime format like "00:00:00"?

Time:01-05

my entity like that

public string CompanyName { get; set; }
public bool IsVerified  { get; set; } = false;
public DateTime permissionStartDate { get; set; }
public DateTime permissionEndDate { get; set; }

my swagger like that

{
  "id": 0,
  "companyName": "string",
  "isVerified": true,
  "permissionStartDate": "2023-01-05T13:53:38.409Z",
  "permissionEndDate": "2023-01-05T13:53:38.409Z"
}

How can I send data from json in 00:00:00 format.

CodePudding user response:

You can write some custom class to modify your response by Overriding JsonConverter by newtonsoft.

So, your custom class should be something like this,

public class CustomTimeConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(DateTime);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        var date = (DateTime)value;
        writer.WriteValue(date.ToString("HH:mm:ss"));
    }
}

here you don't need to implement the ReadJson(), because it's used to deserialize JSON data into an object and CustomTimeConverter is only used to serialize DateTime objects to JSON.

Your response class should be like this,

public class ResponseClass
{
    public string CompanyName { get; set; }
    public bool IsVerified { get; set; } = false;

    [JsonConverter(typeof(CustomTimeConverter))]
    public DateTime permissionStartDate { get; set; }

    [JsonConverter(typeof(CustomTimeConverter))]
    public DateTime permissionEndDate { get; set; }
}

This is a sample code snippet to test the above implementation.

static void Main(string[] args)
{
    var entity = new ResponseClass
    {
        CompanyName = "stackoverflow",
        IsVerified = true,
        permissionStartDate = new DateTime(2023, 1, 5, 12, 0, 0),
        permissionEndDate = new DateTime(2023, 1, 5, 15, 0, 0)
    };

    var json = JsonConvert.SerializeObject(entity , Formatting.Indented);
    Console.WriteLine(json);
}

and output is:

{
  "CompanyName": "stackoverflow",
  "IsVerified": true,
  "permissionStartDate": "12:00:00",
  "permissionEndDate": "15:00:00"
}

CodePudding user response:

The solution would be to write a custom JsonConverter

If you are using System.Text.Json:

using System;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;

public class TimeConverter : JsonConverter<DateTime> {
    public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
        return reader.GetDateTime();
    }

    public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) {
        writer.WriteStringValue(value.ToString("HH:mm:ss", CultureInfo.InvariantCulture));
    }
}

public class Entity {
    public string CompanyName { get; set; }
    public bool IsVerified  { get; set; } = false;
    [JsonConverter(typeof(TimeConverter))]
    public DateTime permissionStartDate { get; set; }
    [JsonConverter(typeof(TimeConverter))]
    public DateTime permissionEndDate { get; set; }
}
  • Related