Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 362 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting DateTime to time ago in Dart/Flutter

#1
The question is how to format a Dart DateTime as a string stating the time elapsed similar to the way you see times displayed on Stack Overflow.

Is there any better method than this

String timeAgo(DateTime d) {
Duration diff = DateTime.now().difference(d);
if (diff.inDays > 365)
return "${(diff.inDays / 365).floor()} ${(diff.inDays / 365).floor() == 1 ? "year" : "years"} ago";
if (diff.inDays > 30)
return "${(diff.inDays / 30).floor()} ${(diff.inDays / 30).floor() == 1 ? "month" : "months"} ago";
if (diff.inDays > 7)
return "${(diff.inDays / 7).floor()} ${(diff.inDays / 7).floor() == 1 ? "week" : "weeks"} ago";
if (diff.inDays > 0)
return "${diff.inDays} ${diff.inDays == 1 ? "day" : "days"} ago";
if (diff.inHours > 0)
return "${diff.inHours} ${diff.inHours == 1 ? "hour" : "hours"} ago";
if (diff.inMinutes > 0)
return "${diff.inMinutes} ${diff.inMinutes == 1 ? "minute" : "minutes"} ago";
return "just now";
}

Thank you and hope it helps others
Reply

#2
I used [timeago][1] for the exact purpose and found it quite useful. It has multiple format and different languages support as well.


[1]:

[To see links please register here]

Reply

#3
I simplify [Paresh's answer][1] by using DateTime extension

create a new dart file called `date_time_extension.dart` and then write code like this


extension DateTimeExtension on DateTime {


String timeAgo({bool numericDates = true}) {
final date2 = DateTime.now();
final difference = date2.difference(this);

if ((difference.inDays / 7).floor() >= 1) {
return (numericDates) ? '1 week ago' : 'Last week';
} else if (difference.inDays >= 2) {
return '${difference.inDays} days ago';
} else if (difference.inDays >= 1) {
return (numericDates) ? '1 day ago' : 'Yesterday';
} else if (difference.inHours >= 2) {
return '${difference.inHours} hours ago';
} else if (difference.inHours >= 1) {
return (numericDates) ? '1 hour ago' : 'An hour ago';
} else if (difference.inMinutes >= 2) {
return '${difference.inMinutes} minutes ago';
} else if (difference.inMinutes >= 1) {
return (numericDates) ? '1 minute ago' : 'A minute ago';
} else if (difference.inSeconds >= 3) {
return '${difference.inSeconds} seconds ago';
} else {
return 'Just now';
}
}


}


and then, use it like this

import 'package:utilities/extensions/date_time_extension.dart'; // <--- import the file you just create

product.createdAt.timeAgo(numericDates: false) // use it on your DateTime property

[1]:

[To see links please register here]

Reply

#4
You can use this Method which will give you times ago.

String convertToAgo(String dateTime) {
DateTime input =
DateFormat('yyyy-MM-DDTHH:mm:ss.SSSSSSZ').parse(dateTime, true);
Duration diff = DateTime.now().difference(input);

if (diff.inDays >= 1) {
return '${diff.inDays} day${diff.inDays == 1 ? '' : 's'} ago';
} else if (diff.inHours >= 1) {
return '${diff.inHours} hour${diff.inHours == 1 ? '' : 's'} ago';
} else if (diff.inMinutes >= 1) {
return '${diff.inMinutes} minute${diff.inMinutes == 1 ? '' : 's'} ago';
} else if (diff.inSeconds >= 1) {
return '${diff.inSeconds} second${diff.inSeconds == 1 ? '' : 's'} ago';
} else {
return 'just now';
}
}
Reply

#5
Hope you got the answer! you just need to past your timestamp value in this method and you get a time ago formatted string.




String getVerboseDateTimeRepresentation(DateTime dateTime) {
DateTime now = DateTime.now().toLocal();

DateTime localDateTime = dateTime.toLocal();

if (localDateTime.difference(now).inDays == 0) {
var differenceInHours = localDateTime.difference(now).inHours.abs();
var differenceInMins = localDateTime.difference(now).inMinutes.abs();

if (differenceInHours > 0) {
return '$differenceInHours hours ago';
} else if (differenceInMins > 2) {
return '$differenceInMins mins ago';
} else {
return 'Just now';
}
}

String roughTimeString = DateFormat('jm').format(dateTime);

if (localDateTime.day == now.day &&
localDateTime.month == now.month &&
localDateTime.year == now.year) {
return roughTimeString;
}

DateTime yesterday = now.subtract(const Duration(days: 1));

if (localDateTime.day == yesterday.day &&
localDateTime.month == now.month &&
localDateTime.year == now.year) {
return 'Yesterday';
}

if (now.difference(localDateTime).inDays < 4) {
String weekday = DateFormat(
'EEEE',
).format(localDateTime);

return '$weekday, $roughTimeString';
}

return '${DateFormat('yMd').format(dateTime)}, $roughTimeString';
}
Reply

#6
A variation on @Alex289's answer

extension DateHelpers on DateTime {

String toTimeAgoLabel({bool isIntervalNumericVisible = true}) {
final now = DateTime.now();
final durationSinceNow = now.difference(this);

final inDays = durationSinceNow.inDays;
if (inDays >= 1) {
return (inDays / 7).floor() >= 1
? isIntervalNumericVisible ? '1 week ago' : 'Last week'
: inDays >= 2
? '$inDays days ago'
: isIntervalNumericVisible
? '1 day ago'
: 'Yesterday';
}

final inHours = durationSinceNow.inHours;
if (inHours >= 1) {
return inHours >= 2
? '$inHours hours ago'
: isIntervalNumericVisible
? '1 hour ago'
: 'An hour ago';
}

final inMinutes = durationSinceNow.inMinutes;
if (inMinutes >= 2) {
return inMinutes >= 2
? '$inMinutes minutes ago'
: isIntervalNumericVisible
? '1 minute ago'
: 'A minute ago';
}

final inSeconds = durationSinceNow.inSeconds;
return inSeconds >= 3 ? '$inSeconds seconds ago' : 'Just now';
}
}
Reply

#7
If you just want to use `Datetime` library this is the way you can do it.
```
void main() {
final currentTime = DateTime.now();
print('Current time: $currentTime');
final threeWeeksAgo = currentTime.subtract(const Duration(days: 21));
print('Three weeks ago: $threeWeeksAgo');
}
```
This is what you get:
```
Current time: 2022-09-29 11:26:58.350
Three weeks ago: 2022-09-08 11:26:58.350
```
Reply

#8
String timeAgoCustom(DateTime d) { // <-- Custom method Time Show (Display Example ==> 'Today 7:00 PM') // WhatsApp Time Show Status Shimila
Duration diff = DateTime.now().difference(d);
if (diff.inDays > 365)
return "${(diff.inDays / 365).floor()} ${(diff.inDays / 365).floor() == 1 ? "year" : "years"} ago";
if (diff.inDays > 30)
return "${(diff.inDays / 30).floor()} ${(diff.inDays / 30).floor() == 1 ? "month" : "months"} ago";
if (diff.inDays > 7)
return "${(diff.inDays / 7).floor()} ${(diff.inDays / 7).floor() == 1 ? "week" : "weeks"} ago";
if (diff.inDays > 0)
return "${DateFormat.E().add_jm().format(d)}";
if (diff.inHours > 0)
return "Today ${DateFormat('jm').format(d)}";
if (diff.inMinutes > 0)
return "${diff.inMinutes} ${diff.inMinutes == 1 ? "minute" : "minutes"} ago";
return "just now";
}

Add This Package --> intl: ^0.17.0



Time Show Example (Today 8:29 PM)
Reply

#9
You can also try this package, [Jiffy][1].

You can get relative time from now

// This returns time ago from now
Jiffy.now().fromNow(); // a few seconds ago

//You can also pass in a DateTime Object or a string or a list
Jiffy.parseFromDateTime(DateTime.now()).fromNow; // a few seconds ago
//or
Jiffy.parseFromDateTime(DateTime(2018, 10, 25)).fromNow(); // a year ago
Jiffy.parse("2020-10-25").fromNow(); // in a year

Manipulating is also simple in Jiffy

var dateTime = Jiffy.now().add(hours: 3, months: 2);

dateTime.fromNow(); // in 2 months

You can also get relative time from a specified time apart from now

Jiffy.parseFromList([2022, 10, 25])
.from(Jiffy.parseFromList([2022, 1, 25])); // in 10 months


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through