C
C#16mo ago
Pedro

❔ problem with DateTime format

guys, the "[DisplayFormat(DataFormatString = "{dd/MM/yyyy}")]" is not working for me: when i add an event or update it, still shows like this { "title": "string", "description": "string", "date": "2023-08-28T22:43:42.452Z", "attendees": [ "string" ] } can somebody help me?
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Net;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Identity;



namespace EventEnroll.Models
{
public class Event
{
[Key]
public int EventId { get; set; }
[Required]
[MinLength(8, ErrorMessage = "Title must be at least 8 characters.")]
public string Title { get; set; } = string.Empty;

[MaxLength(50, ErrorMessage = "Description cannot exceed 50 characters.")]
public string Description { get; set; } = string.Empty;

[Required]
[DisplayFormat(DataFormatString = "{dd/MM/yyyy}")]
public DateTime Date { get; set; }
public string? CreatorId { get; set; }
[ForeignKey("CreatorId")]
public IdentityUser? Creator { get; set; }
// Navigation properties
public ICollection<IdentityUser>? Attendees { get; set; } = new List<IdentityUser>();

}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Net;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Identity;



namespace EventEnroll.Models
{
public class Event
{
[Key]
public int EventId { get; set; }
[Required]
[MinLength(8, ErrorMessage = "Title must be at least 8 characters.")]
public string Title { get; set; } = string.Empty;

[MaxLength(50, ErrorMessage = "Description cannot exceed 50 characters.")]
public string Description { get; set; } = string.Empty;

[Required]
[DisplayFormat(DataFormatString = "{dd/MM/yyyy}")]
public DateTime Date { get; set; }
public string? CreatorId { get; set; }
[ForeignKey("CreatorId")]
public IdentityUser? Creator { get; set; }
// Navigation properties
public ICollection<IdentityUser>? Attendees { get; set; } = new List<IdentityUser>();

}
}
20 Replies
Angius
Angius16mo ago
Database entities should never be concerned with how they're represented. I'd even argue, they should not be represented period, but rather selected into a DTO Also, it's possible that this attribute simply doesn't work with JSON serialization I'd say, the date should be sent as ISO8601 like it happens now, and it should be the concern of the frontend to display it in whatever format is desired
Pedro
PedroOP16mo ago
i tried that
using EventEnroll.Models;
using Microsoft.AspNetCore.Identity;
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;

namespace EventEnroll.Dtos.Event
{
public class AddEventDto
{

public string Title { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime Date { get; set; }
[JsonIgnore]
public string? CreatorId { get; set; }
[Required]
public List<string> Attendees { get; set; } = new List<string>();

}
}
using EventEnroll.Models;
using Microsoft.AspNetCore.Identity;
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;

namespace EventEnroll.Dtos.Event
{
public class AddEventDto
{

public string Title { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime Date { get; set; }
[JsonIgnore]
public string? CreatorId { get; set; }
[Required]
public List<string> Attendees { get; set; } = new List<string>();

}
}
for some reason it still doesnt work so i came with this idea but i dont know if it is a good practice i created a class DateTimeConverter
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace EventEnroll.Utils
{
public class DateTimeConverter : JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return DateTime.Parse(reader.GetString());
}

public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString("dd/MM/yyyy"));
}
}
}
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace EventEnroll.Utils
{
public class DateTimeConverter : JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return DateTime.Parse(reader.GetString());
}

public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString("dd/MM/yyyy"));
}
}
}
Angius
Angius16mo ago
Well, if you really want, you could have the Date be a string and convert the format during selection
Pedro
PedroOP16mo ago
and i added to the program.cs
builder.Services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new DateTimeConverter());
});
builder.Services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new DateTimeConverter());
});
Angius
Angius16mo ago
var stuff = await context.Things
.Where(...)
.Select(t => new ThingDto {
// ...
Date = t.Date.ToString("dd/MM/yyyy"),
})
.FirstOrDefaultAsync();
var stuff = await context.Things
.Where(...)
.Select(t => new ThingDto {
// ...
Date = t.Date.ToString("dd/MM/yyyy"),
})
.FirstOrDefaultAsync();
What I wonder, though, is why not pass the date like it should be In ISO8601 format And let the frontend worry about formatting
Pedro
PedroOP16mo ago
its just the specification for this internship project i got
Angius
Angius16mo ago
Ah, oof, my condolences
Pedro
PedroOP16mo ago
otherwise i would keep it
Angius
Angius16mo ago
Schools and companies utilize the shittiest standards sometimes
Pedro
PedroOP16mo ago
yeah
Angius
Angius16mo ago
In that case, do this
Pedro
PedroOP16mo ago
so, i came with that solution . is this ok?
Angius
Angius16mo ago
Does it work?
Pedro
PedroOP16mo ago
it does haha
Angius
Angius16mo ago
Then it's fine
Pedro
PedroOP16mo ago
just dont know if they will look at it and find strange or bad practices
Angius
Angius16mo ago
Not using ISO8601 is a bad practice already And they clearly don't care So
Pedro
PedroOP16mo ago
hmm yeah ok then
Angius
Angius16mo ago
Even then, this is how you would do it Whatever you needed to format, tapping into the converter options will do the trick And it's fine
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server