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
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
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
I would like to know how a date such as "2022-07-17T01:46:12.632892+05:30" be converted to a Human Readable date in DD/MM/YYYY and hh:mm:ss format? I probably have not surfed through a lot of other questions and suggestions on the Internet but the ones I came across were not of any help. Also, what are such date formats(like the one in question) called?
It is rather straightforward using DateFormat from the package intl which comes with Flutter:
import 'package:intl/intl.dart';
void main() {
final dateTime = DateTime.parse('2022-07-17T01:46:12.632892+05:30').toUtc();
final dateFormat = DateFormat('dd/MM/yyyy HH:mm:ss');
print(dateFormat.format(dateTime)); // 16/07/2022 20:16:12
}
The time has here been converted to UTC to make the example the same for all readers. If not, the created DateTime would be localtime which uses the timezone on the device which the program are running.
If you want to print the time using the timezone offset of 5 hours and 30 minutes, you can do something like this:
import 'package:intl/intl.dart';
void main() {
final dateTime = DateTime.parse('2022-07-17T01:46:12.632892+05:30').toUtc();
final dateFormat = DateFormat('dd/MM/yyyy HH:mm:ss');
print(dateFormat.format(dateTime.add(const Duration(hours: 5, minutes: 30))));
// 17/07/2022 01:46:12
}
I want to check there is any time within certain time. Clearly, I got a list:
List<String> timeSlots = [];
This list returning me times like this: [08:30,09:00,...,22:00]
And I got current phone time. And I am creating listViewBuilder.
I just want to check if current phone time between 08:30-09:00 and I want to set my Card color which it's listViewBuilder's returning widget.
What I would do is use the isAfter & isBefore methods of DateTime. So you will have to create DateTimes out of the first two times :
List<String> timeSlots = ["08:30", "09:00", "22:00"];
DateTime currentTime = DateTime.now();
DateTime date1 = DateTime.parse(
"${currentTime.year}-${currentTime.month < 10 ? '0${currentTime.month}' : currentTime.month}-${currentTime.day} ${timeSlots[0]}:00");
DateTime date2 = DateTime.parse(
"${currentTime.year}-${currentTime.month < 10 ? '0${currentTime.month}' : currentTime.month}-${currentTime.day} ${timeSlots[1]}:00");
print(currentTime.isAfter(date1) && currentTime.isBefore(date2));
There may be other prettier ways though ^^
I have chat app. I want allow user message other user only every other week. For example:
Week 1 (allowed
Week 2 (not allowed)
Week 3 (allowed)
Week 4 (not allowed)
...
How I can do this? I want write conditional so can say:
if (allowedWeek) {
sendMessage
}
Maybe can use something like this?
DateTime.now().isAfter(X) && DateTime.now().isBefore(Y)
How I can tell DateTime this?
May be you can get week of the year by this package, week_of_year, and decide which week to talk which is not:
import 'package:week_of_year/week_of_year.dart';
void main() {
final date = DateTime.now();
print(date.weekOfYear); // Get the iso week of year
print(date.ordinalDate); // Get the ordinal date
print(date.isLeapYear); // Is this a leap year?
}
I am fetching one date-format from json Api and another date is current date i wants the difference in days like this eg;-
server_date=2020-04-18T17:26:33.150;
current_date=2020-05-07;
var days=server_date - current date;
Json Api "CLMM_LAST_ACTIVITY_DT": "2020-04-18T17:26:33.150",
you can use difference method of dataTime.
following code help you more.
String server_date = "2020-04-18T17:26:33.150";
DateTime currentTime = DateTime.now();
int days = currentTime.difference(DateTime.parse(server_date)).inDays;
print(days);
I am struggling to find a way to parse 2 YYYY-MM-DD dates in dart/Flutter. I need to find out whether a given date is before another date in terms of number of days in utc. If they are the same or it is in the future it should return false.
Thanks,
with intl package (https://pub.dev/packages/intl) you can convert those strings to actual date objects and then compare them easily
just compare them with
DateTime date1 = DateTime.now();
DateTime date2 = DateTime.now();
if(date1.millisecondsSinceEpoch > date2.millisecondsSinceEpoch){
return false;
}
return true;
Try out this package, Jiffy
First, parse the string into a Jiffy instance
Jiffy jiffy1 = Jiffy("2019-12-01")..utc();
Jiffy jiffy2 = Jiffy("2019-12-20")..utc();
Next is to check if it is the same or in the future in terms of days
jiffy1.isSameOrBefore(jiffy2, Units.DAY); // true
You can also check if it is the same or in the past in terms of days
jiffy1.isSameOrAfter(jiffy2, Units.DAY); // false
Check out this documentation for more
Hope this helped