Home > Software engineering >  Flutter date and time
Flutter date and time

Time:01-14

I am newbie in learning coding,

hi i want to show show the post created time like 2hours ago or 30 minutes ago like that, how to do that i only have the created time like 2023-01-13 10:35:52", how to convert that with current time or day

CodePudding user response:

you can use the difference method on date and time which give you the difference

final date2 = DateTime.now();
final difference = date2.difference(date);
print(difference.inHours);

from that you can get the difference in hours difference in minutes etc

CodePudding user response:

check the timeago package, it helps implementing what you need

import 'package:timeago/timeago.dart' as timeago;

main() {
    final date = DateTime.now().subtract(Duration(minutes: 50));
    print(timeago.format(date)); // 50 minutes ago
}
  • Related