List of DateTime with time-step - flutter

I have a list of timing for the appointment but I couldn't divide the time into 15-minute steps. This is what I want to achieve.
Time: 6 AM - 8AM to this format: 06:00 -> 06: 15 -> 06:30 -> 06:45-> 07:00 and so on.
I tried but couldn't do it. Any help will be appreciated.

Do you mean something like this?
DateTime now = new DateTime.now();
DateTime first = new DateTime(now.year, now.month, now.day, 6); // today 6AM
final numberOfSlots = 2 * 4 + 1; // 2 hours * 4 (15 minutes) + 1 to go from 6 to 8
final list = List.generate(
numberOfSlots,
(index) => first.add(Duration(minutes: index * 15)),
);
print(list);

Related

How to get dates by days which have intervals of 7 days each

hello guyz am new to flutter and am practicing on dates then i encounter a difficulty on how can i acheive the result same below using a loop or something in order to get this result.
so far i tried is
final now = DateTime.now();
for(var d = 1 ; d <= 5 ; d++){
// Don't know whats next to do here to get the same result below
}
i want result like this
Apr 19, 2022
Apr 26, 2022
May 3, 2022
May 10, 2022
May 17, 2022
can some help me and explain.
Dart's DateTime class has an add function, where you can specify a duration which will be added the current Date.
Therefore, what you can do is:
DateTime now = DateTime.now();
for(var d = 1; d <= 5; d++) {
...
now = now.add(Duration(days: 7)); // here you can specify your interval. Also possible: hours, minutes, seconds, milliseconds and microseconds
}
I'll recommend reading the Docs, e.g. DateTime Docs and Duration Docs

how to create a 30 minutes timeslot.. Im getting the start date and end date from backend

List<WorkingHourModel> workingHours = response['hours'];
int startTime = int.parse(workingHours[0].startTime.split(':')[0]);
int endTime = int.parse(workingHours[0].endTime.split(':')[0]);
int hoursLeft = endTime - startTime + 1;
List<String> hours =
List.generate(hoursLeft, (i) => '${(endTime - i)}:00').reversed
.toList();
print('Hours-------- $hours');
return {
'success': true,
'hours': hours,
};
}
//this will give the time slots as '1:00', '2:00', '3:00', '4:00'
//Start time will be 1:00 and end time will be 4:00.
Heading
But i need 30 mins breakage, like,
'1:00', '1:30', '2:00', '2:30', '3:00', '3:30', '4:00'
Kindly help..
This should generate the list as you want it:
void main(List<String> args) {
// lets assume 9 to 5
int startTime = 9;
int endTime = 17;
int hoursLeft = endTime - startTime + 1;
final hours = List.generate(hoursLeft * 2, (i) => '${endTime - (i/2).truncate()}:${i%2 == 1 ? '00' : '30'}').reversed.toList();
print('Hours-------- $hours');
}
Hours-------- [9:00, 9:30, 10:00, 10:30, 11:00, 11:30, 12:00, 12:30, 13:00, 13:30, 14:00, 14:30, 15:00, 15:30, 16:00, 16:30, 17:00, 17:30]
Generally speaking, if you have more complicated calculations (what happens if someone works 18:00 to 05:00 in the nightshift?) you may want to use the proper datatypes instead of integers and string concatenation.

Get the Month and Year X number of months from now

I am attempting to generate a PageView that will display the Month and Year related to the number of times you swipe in either directon.
Example 1:
I swipe right twice, so I get Feb 2021
Example 2:
I swipe left 12 times, so I get April 2020
I have attempted to create a DateTime.now() and subtract an integer of months, but I'm not having much luck. I have looked at various plugins like DateUtils, but again no luck.
I have been at what should be a simple solution for while now and would appreciate a guidance.
The closet I get is the following which requires me to know the days in each month which isn't ideal
(DateTime.now().subtract(Duration(days: 90)).toString())
From DataTime docunamtion:
Returns a new [DateTime] instance with [duration] added to [this].
var today = DateTime.now();
var fiftyDaysFromNow = today.add(const Duration(days: 50));
// adds 1 days
DateTime _future = DateTime.now().add(const Duration(days: 1));
//substracts 1 day
DateTime _tomorrow2 = DateTime.now().subtract(const Duration(days: 1));
Also this, credit ,define the base time, let us say:
var date = new DateTime(2018, 1, 13);
Now, you want the new date:
var newDate = new DateTime(date.year, date.month - 1, date.day);
And you will get
2017-12-13
Y'all are going about it wrong. Presuming 24 hours in a day, or 30 days in a month, is just wrong. Here's how to always get midnight the first of the month, 7 months before today:
void main() {
var n = DateTime.now();
print(DateTime(n.year, n.month - 7, 1));
}
Just use DateTime constructors. They wrap around just fine. Works at month's end as well:
void main() {
var n = DateTime(2021, 2, 28);
print(DateTime(n.year, n.month, n.day + 1));
n = DateTime(2020, 2, 28);
print(DateTime(n.year, n.month, n.day + 1));
}
Which correctly shows 3/1 for 2021, and 2/29 for 2020, as it was a leap year.
Stop adding 24-hour days! I've got a video that explains why.... https://www.youtube.com/watch?v=usFSVUEadyo
And here's a video that goes into this with more detail: Proper Month and Day Arithmetic in Dart and Flutter: youtu.be/LpoBYgzKVwU

Flutter: Find the difference between two dates in Years, Months including leap years

I am working on an application in flutter where I have to get the difference between two dates in Years, Months and Dates
like below Ex.
final _date1 = DateTime(2019, 10, 15);
final _date2 = DateTime(2020, 12, 20);
Answer Difference = 1 Year, 2 Month, 5 Days
I searched a lot but unfortunately din't get a proper solution to calculate the difference between two dates in Years, Months including leap years.
Note: I know how to get the difference between two dates in Days i.e
final _bd = DateTime(2020, 10, 12);
final _date = DateTime.now();
final _difference = _date.difference(_bd).inDays;
You may customize the following code as per your requirement to return a string or object or make it an extension:
void getDiffYMD(DateTime then, DateTime now) {
int years = now.year - then.year;
int months = now.month - then.month;
int days = now.day - then.day;
if (months < 0 || (months == 0 && days < 0)) {
years--;
months += (days < 0 ? 11 : 12);
}
if (days < 0) {
final monthAgo = DateTime(now.year, now.month - 1, then.day);
days = now.difference(monthAgo).inDays + 1;
}
print('$years years $months months $days days');
}

How to calculate time difference between two dates using LocalDateTime in Java 8?

There are numerous answers given but I am unable to find compatible with my situation. I need to find difference of 8 hours in time as well as on date change too. Like if time is greater then 8 hours then do not execute something .
Do we have any method which achieve the same in LocalDateTime in Java-8?
I have tried to use following but unable to achieve the same.
LocalDateTime fromDateTime = LocalDateTime.of(2017, 07, 07, 07, 00, 55);
LocalDateTime toDateTime = LocalDateTime.now();
LocalDateTime tempDateTime = LocalDateTime.from(fromDateTime);
long years = tempDateTime.until(toDateTime, ChronoUnit.YEARS);
tempDateTime = tempDateTime.plusYears(years);
long months = tempDateTime.until(toDateTime, ChronoUnit.MONTHS);
tempDateTime = tempDateTime.plusMonths(months);
long days = tempDateTime.until(toDateTime, ChronoUnit.DAYS);
tempDateTime = tempDateTime.plusDays(days);
long hours = tempDateTime.until(toDateTime, ChronoUnit.HOURS);
tempDateTime = tempDateTime.plusHours(hours);
long minutes = tempDateTime.until(toDateTime, ChronoUnit.MINUTES);
tempDateTime = tempDateTime.plusMinutes(minutes);
long seconds = tempDateTime.until(toDateTime, ChronoUnit.SECONDS);
System.out.println("" + java.time.Duration.between(tempDateTime, toDateTime).toHours());
System.out.println(years + " years "
+ months + " months "
+ days + " days "
+ hours + " hours "
+ minutes + " minutes "
+ seconds + " seconds.");
It is difficult to check on time and date separately.
Initially I coded it like but it does not looks correct:
return openBrat!=null
&& openBrat.until(LocalDateTime.now(), ChronoUnit.DAYS) == 0 &&
openBrat.until(LocalDateTime.now(), ChronoUnit.HOURS) >= 8
&& openBrat.until(LocalDateTime.now(), ChronoUnit.Minutes) >= 0;
Could anyone please suggest how to subtract like:
2017 07 06 23:30:00 - 2017 07 07 01:30:00 - Should return 2 hours.
The following prints 2, just like you'd expect.
LocalDateTime ldt1 = LocalDateTime.of(2017, 07, 06, 23, 30, 00);
LocalDateTime ldt2 = LocalDateTime.of(2017, 07, 07, 1, 30, 00);
System.out.println(Duration.between(ldt1, ldt2).toHours());
There is nothing wrong with your code. If you don't get the outcome you expect, you may need to check if your expectation is correct.
To do something when the difference is less than 8 hours, you can do something like this:
LocalDateTime ldt1 = LocalDateTime.of(2017, 07, 06, 23, 30, 00);
LocalDateTime ldt2 = LocalDateTime.of(2017, 07, 07, 1, 30, 00);
Duration d1 = Duration.between(ldt1, ldt2);
Duration d2 = Duration.ofHours(8);
if (d1.compareTo(d2) > 0) {
System.out.println("do nothing");
} else {
System.out.println("do something");
}
My understanding is that you want to get the difference between those 2 dates and "break" it in terms of how many hours, minutes and seconds this difference is.
You can use the Duration class as already explained. But the Duration calculates the difference in seconds and nanoseconds, so you'll have to do some math to get this amount of seconds in separate fields:
LocalDateTime d1 = LocalDateTime.of(2017, 7, 6, 23, 30, 0);
LocalDateTime d2 = LocalDateTime.of(2017, 7, 7, 7, 0, 55);
Duration duration = Duration.between(d1, d2);
// total seconds of difference (using Math.abs to avoid negative values)
long seconds = Math.abs(duration.getSeconds());
long hours = seconds / 3600;
seconds -= (hours * 3600);
long minutes = seconds / 60;
seconds -= (minutes * 60);
System.out.println(hours + " hours " + minutes + " minutes " + seconds + " seconds");
In Java 9 and later, you can call the new Duration::to…Part methods to get number of days, hours, minutes, or seconds rather than calculate the numbers yourself.
The output will be:
7 hours 30 minutes 55 seconds
And the variables hours, minutes and seconds will have the respective values of 7, 30 and 55. If you also want the nanoseconds, just call duration.getNano() to get the respective value (in the example above, it's 0).
If I test with different values:
LocalDateTime d1 = LocalDateTime.of(2017, 7, 6, 23, 30, 0);
LocalDateTime d2 = LocalDateTime.of(2017, 7, 7, 1, 30, 0);
The result will be:
2 hours 0 minutes 0 seconds
If you just want the difference in hours, you can use:
ChronoUnit.HOURS.between(d1, d2);
You can optionally use Math.abs to avoid negative values.
This will return the difference in hours, ignoring the remaining minutes and seconds: in the first example (d2 = LocalDateTime.of(2017, 7, 7, 7, 0, 55)) it will return 7 and in the second example (d2 = LocalDateTime.of(2017, 7, 7, 1, 30, 0)) it will return 2.