Flutter Countdown - flutter

I'd like to create a timer for a flutter website that counts down to a certain date.
e.g.
120 Days 12 Hours 45 Mins 15 Secs
Is that possible and can anyone help?
I haven't got a clue where to start with this one..

You can use this package
https://pub.dev/packages/countdown_flutter
Note: In this package you will see a duration property. You should locate time difference between nowDate and the other date
var nextDate = DateTime(2023, 10, 29);
var nowDate = DateTime.now();
var difference = nowDate.difference(nextDate);

Related

Flutter hours and minute count issue

I need to sum the hours and minutes so I am doing this like ill convert hours in second and minutes in second then sum it
var totalServiceSeconds = minsSeconds + hoursSeconds;
var c = Duration(seconds: totalServiceSeconds);
print('c ${c.toString()}');
it's showing c 25:05:00.000000 which is correct
Know I need to show this as hours and minutes in the text widget. So I am converting to DateTime like this
var format = DateFormat("HH:mm");
DateTime totalServiceTime = format.parse(c.toString());
But it's printing it like this totalServiceTime 1970-01-02 01:05:00.000
This issue is only when the hours are 24 or more. If my hours are 24 then it's showing 0 and if greater than 24 then it's showing 1 2 so on. I know it because it's considering 24 as 0 but what can I do about this?
I want to show 24 if it's 24 hours or if greater than 24 like 26 need to show 26.
You do not want to convert it into a DateFormat because time steps of 24 hours is how they count a full day. Instead you should format var c as shown below
var totalServiceSeconds = minsSeconds + hoursSeconds;
var c = Duration(seconds: totalServiceSeconds);
print('c ${c.toString()}');
String FormatDuration = "${c.inHours}:${c.inMinutes.remainder(60)}:${(c.inSeconds.remainder(60))}";
print(FormatDuration);
String FormatDuration2 = "${c.inHours} hours ${c.inMinutes.remainder(60)} minutes ${(c.inSeconds.remainder(60))} seconds";
print(FormatDuration2);
The output will then be
c 25:05:00.000000 <-------previous
25:5:0 <-------new option 1
25 hours 5 minutes 0 seconds <-------new option 2

Date Picker minimum date and minute interval visual error

I have a UIDatePicker and set minimum date and minute interval, however there is a visual bug when selecting date. Minimum minute is selectable but appears with the non-selectable grey color.
My code:
#IBOutlet weak var datePicker: UIDatePicker!
override func viewDidLoad() {
super.viewDidLoad()
datePicker.preferredDatePickerStyle = .wheels
datePicker.minuteInterval = 5
datePicker.minimumDate = Date(timeIntervalSinceNow: 7200) // 7200 is equal to 2 hours
}
Screenshot:
I think the date picker is somewhat confused because the minimum date isn't a multiple of five minutes. For example, if you opened the app at 12:34, then Date(timeIntervalSinceNow: 7200) is going to be 14:34, and that doesn't correspond to any date on the date picker, which might be why it is behaving weirdly like this. I don't think this intended though.
Anyway, if you always give it a date that is a multiple of 5 minutes (or whatever minuteInterval you have), then it will work as expected.
From your description of the desired behaviour, it seems like you want to find the next five minute mark if the current minute component is not divisible by 5, then add 2 hours. You can do:
let calendar = Calendar.current
let now = Date()
let currentMinute = calendar.component(.minute, from: now)
let mod = currentMinute % 5
let minutesUntilNextMultipleOf5 = mod == 0 ? mod : 5 - mod
let minDate = calendar.date(byAdding:
DateComponents(hour: 2, minute: minutesUntilNextMultipleOf5),
to: now)
datePicker.minimumDate = minDate

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

Why the Jiffy package show the wrong week number

I see a strange behavior with the Jiffy package (link). If I run the following code I get the following output (the first is correct, the second is wrong)
42
43
final refDate = new DateTime(2020, 10, 18); // 18 Oct 2020
int weekNumber(DateTime date) {
int dayOfYear = int.parse(DateFormat("D").format(date));
return ((dayOfYear - date.weekday + 10) / 7).floor();
}
print(weekNumber(refDate));
print(Jiffy(refDate).week.toString());
Can anyone explain when? I see the .week function call the same formula... (link)
Thanks!
This seems to be the intended behaviour of the package, ie weeks to start on Sunday and end on Saturday. Hopefully the developer will give an option to set when the week should start

Use the first day of the month as 1(integer)

I'm trying to build my first app using flutter framework. The app is about my "End of Year Challenge". It started from 1st Sept 2019 and will last till the end of this year.
What I'm trying to achieve is - I want to display the current day number of the challenge period. eg: 1st Sept is Day 1, 30th Sept is Day 30 and 1st Oct is Day 31 and so on.
I'm trying to get the first day of Sept and assign it to 1. Then using a loop I want the app to update the day to the current day. The loop will stop once the current day equals to 122 (as this would be the last day of the challenge)
Here's the screenshot of the UI
final firstSeptember = DateTime.utc(2019, DateTime.september, 1);
static const totalNumberOfDays = 122;
int noOfDay(){
int dayOne = firstSeptember.day; // I'm just trying codes, IDK the actual code/business logic
return dayOne;
}
In function you'd use
int noOfDay(){
var todayDate = DateTime.now();
final firstSeptember = DateTime.utc(2019, DateTime.september, 1);
var difference = todayDate.difference(firstSeptember);
return difference.inDays + 1;
}
Explanation:
Get today's date
var todayDate = DateTime.now();
You already have start date which is
final firstSeptember = DateTime.utc(2019, DateTime.september, 1);
All you need to do is subtraction.
var difference = todayDate.difference(firstSeptember);
int daysCompleted = difference.inDays + 1;