Currently I made a form for the user to select the symptoms that they present, these symptoms are stored in firestore along with the date on which the data was saved, what I want is that when displaying this data the date appears and if I mate but not with the format as it is in firestore, I will place a photo of how it is in firestore and how it shows me in the app.
DateTime dateTime = DateTime.now();
DateTime dateTime = DateTime.now();
void _showDatePicker() {
showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2000),
lastDate: DateTime(2025),
).then((value) {
setState(() {
dateTime = value!;
});
});
}
Here I store them:
ElevatedButton(
onPressed: () async {
await users.add({
'Fatiga': fatiga,
'Miccion': miccion,
'Flujo Vaginal': flujoVaginal,
'Estreñimiento': estrenimiento,
'Acidez Gastrica': acidezGastrica,
'Sangrado Nasal': sangradoNasal,
'Sangrado de encias': sangradoEncias,
'Hinchazon': hinchazon,
'Problemas respiratarios': problemasRespiratorios,
'Fecha ingreso sintoma': dateTime
}).then((value) => (value) => print('User added'));
},
child: Text("Guardar"))
This is where I generate the date and store it.
ListView.builder(
itemCount: data.size,
itemBuilder: (context, index) {
return Text('''
'Los sitomas que presenta son:
Fatiga: ${data.docs[index]['Fatiga']}
Acidez Gastrica: ${data.docs[index]['Acidez Gastrica']}
Fecha de sintoma: ${data.docs[index]['Fecha ingreso sintoma']}
''');
And this is where I show it.
Here is how it is saved in firestore.
But in the app it shows me like this:
Firebase stores the time as a Timestamp, which is not the same as the DateTime used by Dart/Flutter.
What you would have to do is pass from Timestamp to DateTime:
DateTime.fromMicrosecondsSinceEpoch(data.docs[index]['Fecha ingreso sintoma']).toString();
Then to format the date, you need an external package, which you'll need to install in the pubspec: intl .
Once installed, to format the date, you have to follow a process like this:
Source: https://stackoverflow.com/questions/16126579/how-do-i-format-a-date-with-dart
To the class
DateFormat
you will have to pass a string of characters with the output format you want. I leave you a link so that you can delve deeper into the options that DateFormat offers you to format: https://pub.dev/documentation/intl/latest/intl/DateFormat-class.htmlRemember that if my question has helped you and you think it is the correct one, mark it as such. In this way, you encourage good use of the site and that other people can benefit from your question.