-1

I am trying to send an DateTime instance per Json. I know how to decode DateTime from a Json file. But how can I encode it correctly?

DateTime birthdate; Map<String, dynamic> toJson() => { 'birthDate': birthdate, };

Does not work

  • 2
    It depends on how you want a date/time to be represented in your JSON structure. There are many possible ways. – jamesdlin Oct 17 '21 at 19:45
  • Can you show me a regular used one? I think it will work – Markus Handel Oct 17 '21 at 19:47
  • There is no "regular one". You can choose to store a date/time as an integer representing the number of seconds from the Unix epoch, an integer for the number of milliseconds, a `YYYY-MM-DD` string, a `DD-MM-YYYY` string, a `MM-DD-YYYY` string, ... – jamesdlin Oct 17 '21 at 20:53
  • This is solve https://stackoverflow.com/a/60389537/12212907 – Ameer Amjed Oct 17 '21 at 21:01

1 Answers1

0

Here is a sample:

   Map<String, dynamic> toJson() {
      DateTime myBirthday = DateTime.now();
      Map<String, dynamic> jsonObject = {
         'birthday': myBirthday,
      };
      return jsonObject;
   }
Canada2000
  • 1,688
  • 6
  • 14