You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
873 B

1 year ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.Json;
  6. using System.Threading.Tasks;
  7. namespace Shentun.Utilities
  8. {
  9. public class JsonDateTimeConverter : System.Text.Json.Serialization.JsonConverter<DateTime>
  10. {
  11. private readonly string _dateFormat;
  12. public JsonDateTimeConverter(string dateFormat)
  13. {
  14. _dateFormat = dateFormat;
  15. }
  16. public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
  17. {
  18. return DateTime.ParseExact(reader.GetString(), _dateFormat, null);
  19. }
  20. public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
  21. {
  22. writer.WriteStringValue(value.ToUniversalTime().ToString(_dateFormat));
  23. }
  24. }
  25. }