I have a scenario where user selects a start date and end date and user also selects a specific day I need to show that specific day with date that occurs between them.
I tried the Intl package difference method but did not work
You can use this method. Takes the start and end date and also the weekday. Note, can pass in 3 as an int or 'DateTime.wednesday' as the argument.
Note, idea based on mirkancal's answer in this thread
List<DateTime> getAllDatesOfAWeekday(
{required DateTime startDate,
required DateTime endDate,
required int weekday}) {
List<DateTime> allDates = [];
for (int i = 0; i <= endDate.difference(startDate).inDays; i++) {
if (startDate.add(Duration(days: i)).weekday == weekday) {
allDates.add(startDate.add(Duration(days: i)));
}
}
return allDates;
}
I am totally new with flutter and I do not understand how can I resolve a problem.
I'm actually working to a kart race app and:
I need to read a string like 1:02.456
Convert in some kind of time
Compare with another string similar to first one
Go to do something
es:
blap = null;
if(1:02.456 < 1:03.589){
blap = '1:02.456';
} else {
blap = '1:03.589;
}
I read on the web that I ca use the class DateTime, but every time I try to convert the string in an object of that class, I do not get wat I want.
There is a better way?
Thank you.
If you are working on a kart race app probably you need to use Duration, not DateTime.
This is one way to convert a string like yours into Duration
Duration parseDuration(String s) {
int hours = 0;
int minutes = 0;
int micros;
List<String> parts = s.split(':');
if (parts.length > 2) {
hours = int.parse(parts[parts.length - 3]);
}
if (parts.length > 1) {
minutes = int.parse(parts[parts.length - 2]);
}
micros = (double.parse(parts[parts.length - 1]) * 1000000).round();
return Duration(hours: hours, minutes: minutes, microseconds: micros);
}
Then, to compare two Duration in the way you wanted, this is an example:
String blap;
Duration time1=Duration(hours: 1),time2=Duration(hours: 2);
if(time1.compareTo(time2)<0){
//time2 is greater than time1
blap=time1.toString();
}else{
blap=time2.toString();
}
I have 2 dates with time. I want to get the latest date from these 2 dates.
for example, 1st date and time is "2021-07-14 11:13:02" and 2nd is "2021-04-25 10:24:08". From these i want to find the latest date. How can i get this in Flutter and Dart.
You can do by using DateTime.parse and isAfter(), isBefore() method.
void main() {
print(getLatestDate('2021-07-14 11:13:02', '2021-04-25 10:24:08'));
}
DateTime getLatestDate(String a, String b) {
DateTime dateA = DateTime.parse(a);
DateTime dateB = DateTime.parse(b);
if (dateA.isBefore(dateB)) {
return dateB; // If you want String, return b
} else {
return dateA; // If you want String, return a
}
}
I am trying to use a PageView.builder in my application and i wanted each page to display a particular date. I have defined a list of type DateTime : List<DateTime> _month;. how do i store all the days in a particular month (30 days) in the list _month?
The stored dates must be of type DateTime in order to implement this in my application.
This code will do what you need. I don't know the entire structure of your code so I just wrote this example!
void main() {
int month = 1;
DateTime start = DateTime(2019,month);
DateTime end = DateTime(2019,month+1);
int c = (end.toUtc().difference(start.toUtc()).inDays);
List<DateTime> _month = [];
_month.addAll(List.generate(c,(index) => start.toUtc().add(Duration(days:index)).toLocal()));
print(_month);
}
I need to store and compare dates (without times) in my app, without caring about time zones.
I can see three solutions to this:
(date1.year == date2.year && date1.month == date2.month && date1.day == date2.day)
This is what I'm doing now, but it's horrible verbose.
date1.format("YYYYMMDD") == date2.format("YYYYMMDD")
This is still rather verbose (though not as bad), but just seems inefficient to me...
Create a new Date class myself, perhaps storing the date as a "YYYYMMDD" string, or number of days since Jan 1 1980. But this means re-implementing a whole bunch of complex logic like different month lengths, adding/subtracting and leap years.
Creating a new class also avoids an edge case I'm worried about, where adding Duration(days: 1) ends up with the same date due to daylight saving changes. But there are probably edge cases with this method I'm not thinking of...
Which is the best of these solutions, or is there an even better solution I haven't thought of?
Since I asked this, extension methods have been released in Dart. I would now implement option 1 as an extension method:
extension DateOnlyCompare on DateTime {
bool isSameDate(DateTime other) {
return year == other.year && month == other.month
&& day == other.day;
}
}
You can use compareTo:
var temp = DateTime.now().toUtc();
var d1 = DateTime.utc(temp.year,temp.month,temp.day);
var d2 = DateTime.utc(2018,10,25); //you can add today's date here
if(d2.compareTo(d1)==0){
print('true');
}else{
print('false');
}
DateTime dateTime = DateTime.now();
DateTime _pickedDate = // Some other DateTime instance
dateTime.difference(_pickedDate).inDays == 0 // <- this results to true or false
Because difference() method of DateTime return results as Duration() object, we can simply compare days only by converting Duration into days using inDays property
The easiest option is just to use DateUtils
For example
if (DateUtils.isSameDay(date1, date2){
print('same day')
}
isSameDay takes in 2 DateTime objects and ignores the time element
I am using this function to calculate the difference in days.
Comparing dates is tricky as the result depends not just on the timestamps but also the timezone of the user.
int diffInDays (DateTime date1, DateTime date2) {
return ((date1.difference(date2) - Duration(hours: date1.hour) + Duration(hours: date2.hour)).inHours / 24).round();
}
Use instead the package: dart_date
Dart Extensions for DartTime
dart_date provides the most comprehensive, yet simple and consistent toolset for manipulating Dart dates.
dart_date
DateTime now = DateTime.now();
DateTime date = ....;
if (date.isSameDay(now)) {
//....
} else {
//....
}
Also here the difference in days :
int differenceInDays(DateTime a, DateTime b) => a.differenceInDays(b);
Use isAtSameMomentAs:
var date1 = DateTime.now();
var date2 = date1.add(Duration(seconds: 1));
var isSame = date1.isAtSameMomentAs(date2); // false