Flutter How to check there is any time within certain time? - flutter

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 ^^

Related

Flutter: add date and time from two different variables to convert to UTC

I want to merge these two date and time variables and have another variable called var toUTC.
I have two variables for date and time from the Date Picker and Time Picker.
var selectedDate;
var selectedTime;
print(selectedDate); // 2021-02-06 00:00:00.000
print(selectedTime); // 1:15:00.000000
The var selectedDate has the time but I don't want to use this. I want to replace it with the value from var selectedTime.
What I am trying to do is
print(toUTC) // 2021-02-06 1:15:00.00000 <---- merge selectedDate and selectedTime
to convert to UTC time.
I've tried to take only 2021-02-06 and merge with selectedTime, but I failed to convert to UTC.
final toUTC = DateTime(selectedDate.year, selectedDate.month, selectedDate.year,
selectedTime.hour, selectedTime.minute);
Or if its something you are going to need to do regularly you could make an extension for it. The below does it by passing the hour and minute in as separate variables, but you could change that to be another DateTime value if you would prefer.
extension DateTimeExtensions on DateTime {
/// Creates a new date time with the given date but with the time
/// specified from [time]
DateTime withTime([int hour = 0, int minute = 0]) =>
DateTime(this.year, this.month, this.day, hour, minute);
}
To convert to UTC there is the method toUtc() on the DateTime class. I'm not sure if you've just not spotted that or if there is an issue I'm not seeing?

Days calculation difference in flutter json Api date - current date

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);

Parsing date in dart/Flutter to calculate day difference

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

Change times of a reservation (timestamp) using Listbox in GWT

I want to implement a function (with GWT) to change the start- and end-date/time of a reservation!
The start- & end-date of the reservation is saved as a timestamp.
The reservations are saved in a mySQL DB.
The user can choose the day in a datepicker:
final DatePicker chooseDay = new DatePicker();
..
Date startDate = reservation.getStartDate();
..
chooseDay.setValue(startDate);
..
2 ListBoxes:
final ListBox startTime = new ListBox();
final ListBox endTime = new ListBox();
..
startTime.addItem("08:00");
startTime.addItem("08:30");
startTime.addItem("09:00"); (til 18:00)
...
endTime.addItem("08:00");
endTime.addItem("08:30");
endTime.addItem("09:00"); (til 18:00)
I now have the problem, that i don't know how to change between the formats. Reservations are saved as a timestamp, but how can i change just day and hour/minute?
I am a beginner and it would be really nice if you can help me. Thank you :)
Use can use JsDate on the client side:
JsDate jsDate = JsDate.create(startDate.getTime());
int hour = jsDate.getHours();
int mins = jsDate.getMinutes();

How do I convert a Date to an int?

I have this
DateFormat dateFormat = new SimpleDateFormat("MM");
Date date = new Date();
I know the value as 10 but have got no idea how to print it out. How do I convert it to an int? If there even is a way?
date.getMonth()
it is deprecated - you should be using Calendar, but if you don't want to change, that'll do it. It is 0 indexed, so remember to change the resulting value appropriately.
Javadoc