Home > Enterprise >  how to convert a time eg- 22:00:00 to a timestamp include next day date in Flutter
how to convert a time eg- 22:00:00 to a timestamp include next day date in Flutter

Time:03-05

I want to ask how to convert a given time for example 22:00:00 into a timestamp and also add the next day date to it while converting into a time stamp in flutter. Thank You

CodePudding user response:

You can convert a Date string to a timestamp

 convertDateTimeToTimestamp(String yourDateTime, [Duration? extraDuration]) {
    
    DateTime date = DateTime.parse(yourDateTime);
    if (extraDuration != null) {
      date = date.add(extraDuration);
    }
    return date.microsecondsSinceEpoch;
  }

then your example with one additional day (next day) can be:

main() {
  final timestamp = convertDateTimeToTimestamp(
    "2022-03-05 22:00:00",
    Duration(days: 1),
  );
  print(timestamp); //output: 1646600400000000
  
  // try to check the converted timestamp with addition duration in the example above, it's only one day
  DateTime date = DateTime.fromMicrosecondsSinceEpoch(timestamp);
  print('${date.year}-${date.month}-${date.day} ${date.hour}:${date.minute}:${date.second}'); //output: 2022-3-6 22:0:0
}


you can use intl package and format your datetime easier.

CodePudding user response:

Timestamp data type is defined in cloud_firestore.

What do you mean by timestamp?

  • Related