Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

342
Vistas
Parsing Date from MongoDb in Flutter always fails

I'm trying to parse a Date signUpDate field from MongoDb User object received via http request into a Flutter DateTime signUpDate but it always fails. In my User Schema I have signUpDate: { type: Date, required: false, default: Date.now },. In my Flutter factory User.fromMongoDB(Map<String, dynamic> map) helper I'm using signUpDate: DateTime.parse(map['signUpDate']). When printing http response signUpDate is "signUpDate":"2021-12-15T11:10:01.521Z". Is it just the format coming back from mongo not parsable by DateTime parser or I'm parsing it wrongly? Many thanks.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Here the code for parsing ISO8601 string to Date time just passing what format you need.

import 'package:intl/intl.dart';

void main() {
  var data = "2021-12-15T11:10:01.521Z";

  DateTime dateTime = getFormattedDateFromFormattedString(
      value: data,
      currentFormat: "yyyy-MM-ddTHH:mm:ssZ",
      desiredFormat: "yyyy-MM-dd HH:mm:ss");
  
  print(dateTime); //2021-12-15 11:10:01.000
}

getFormattedDateFromFormattedString(
    {required value,
    required String currentFormat,
    required String desiredFormat,
    isUtc = false}) {
  DateTime? dateTime = DateTime.now();
  if (value != null || value.isNotEmpty) {
    try {
      dateTime = DateFormat(currentFormat).parse(value, isUtc).toLocal();
    } catch (e) {
      print("$e");
    }
  }
  return dateTime;
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda