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
Asked
Active
Viewed 111 times
0
-
[The answer](https://stackoverflow.com/a/69712761/) I pointed you to in [your other question](https://stackoverflow.com/q/71344083/) shows how to: 1. Parse a string that has a time. 2. How to create a `DateTime` object with it. 3. How to add a one day to that `DateTime`. Do you have specific questions about it? – jamesdlin Mar 04 '22 at 00:00
-
Please provide enough code so others can better understand or reproduce the problem. – Community Mar 04 '22 at 07:04
2 Answers
0
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.

Majid
- 2,507
- 2
- 19
- 18
0
Timestamp data type is defined in cloud_firestore.
What do you mean by timestamp?

Abhishek Thakur
- 99
- 6