2

everyone i am using MongoDB as my backend , i don't have any clues how can i convert DateTime type to ISODate MongoDB , i try to look through document and searching , i can't find anything about it , do you have any idea about this ? thank in advance

Heng YouSour
  • 133
  • 1
  • 11
  • @JahidulIslam yeah , here what i'm doing right now i want to insert date type from flutter to mongoDB and then get it back as type datetime , but i don't know how can i convert dateTime type on json serialzable package , can you show me how can i do it ? – Heng YouSour Nov 22 '21 at 04:11

1 Answers1

2

you can convert dart DateTime to ISO8601Date format with code like this :

DateTime now = DateTime.now();
String isoDate = now.toIso8601String(); 
Mojtaba Ghiasi
  • 823
  • 6
  • 16
  • no , it isn't what i want , i just want to know how can i convert DateTime type to ISODate type on mongoDB by json_serializable package – Heng YouSour Nov 22 '21 at 08:56
  • you can, do not use the json_serializable package and make your JSON as a string then encrypts to JSON. – Mojtaba Ghiasi Nov 22 '21 at 11:39
  • i can not do that , my whole project completely using json_serializable package , any idea or choice ? – Heng YouSour Nov 23 '21 at 01:11
  • nvm, thank you , it solved – Heng YouSour Nov 26 '21 at 06:49
  • @HengYouSour how did you solve the problem? – saibot May 19 '22 at 10:07
  • 1
    @saibot here what i did `@JsonKey( fromJson: CoreModelConverter.convertDateTimeForModel, toJson: CoreModelConverter.convertDateTimeForModel, disallowNullValue: true) final DateTime? createdAt; ` `class CoreModelConverter { static DateTime? convertDateTimeForModel(DateTime? dateTime) => dateTime; }` – Heng YouSour May 24 '22 at 06:30