i want to take duration in months how to i take
This is my code
labelText: 'End Date',
hintText: ' yyyy-MM-dd'),
onTap: () async {
final int dur = int.parse(durationController.text);
var stDate = DateTime.parse(startDateController.text);
final DateFormat formatter = DateFormat('yyyy-MM-dd');
final String formatted = formatter.format(stDate);
final double days = 31;
final int day = days.toInt();
print(formatted);
DateTime thirtyDaysFromNow =
stDate.add(new Duration(days: dur * day - 7));
print('thirty days $thirtyDaysFromNow');
DateTime? pickedDate = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(1991),
lastDate: DateTime(2101),
).then((pickedDate) {
if (pickedDate != null) {
String formattedDate =
DateFormat('yyyy-MM-dd').format(pickedDate);
print(formattedDate);
setState(() {
endDateController.text = formatter.format(
DateTime.parse(thirtyDaysFromNow.toString()));
});
print(endDateController.text);
} else {
print("Date is not selected");
}
});
}
Related
Currently initialDate is set to DateTime.now() initially today's date must shown but when I select any date and again opens the Date Picker initial date should be the one which I have selected previously.
How to do this:
child: TextField(
onTap: () async {
DateTime? pickedDate = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(
1991), //DateTime.now() - not to allow to choose before today.
lastDate: DateTime(2025),
// onConfirm:widget.onChanged,
).then((pickedDate) {
if (pickedDate != null) {
// print(
// pickedDate); //pickedDate output format => 2021-03-10 00:00:00.000
String formattedDate =
DateFormat('yyyy-MM-dd').format(pickedDate);
print(formattedDate);
setState(() {
startDtcontroller.text = formattedDate;
//set output date to TextField value.
});
//print(startDtcontroller.text);
//formatted date output using intl package => 2021-03-16
//you can implement different kind of Date Format here according to your requirement
// DateFormat df = new DateFormat("yyyy-MM-dd");
// String stDate = df.format(pickedDate);
// print(stDate);
widget.onChanged(formattedDate);
} else {
print("Date is not selected");
}
});
You need to save that value somewhere...
DateTime? selectedDateTime;
...
child: TextField( onTap: () async {
DateTime? pickedDate = await showDatePicker(
context: context,
initialDate: selectedDateTime ?? DateTime.now(),
...).then(pickedDate) { setState(() => selectedDateTime = pickedDate);}
I built datepicker function. when the date selected then should calculate the age and should display date and age. The date display correctly but age not show.
code:- I called the calAge() method in the showdatepicker method
void showDatePicker() {
DateTime mindate = new DateTime(now.year - 2, now.month, now.day);
DateTime maxdate = new DateTime(now.year - 1, now.month, now.day);
showCupertinoModalPopup(
context: context,
builder: (BuildContext builder) {
return Container(
height: MediaQuery.of(context).copyWith().size.height * 0.25,
color: Colors.white,
child: CupertinoDatePicker(
mode: CupertinoDatePickerMode.date,
initialDateTime: mindate,
onDateTimeChanged: (value) {
if (value != null && value != selectedDate)
setState(() {
selectedDate = value;
calAge();
});
},
maximumDate: maxdate,
minimumDate: mindate,
),
);
});
}
calAge() method
void calAge(DateTime selectedDate) {
DateTime currentDate = DateTime.now();
int age = currentDate.year - selectedDate.year;
int month1 = currentDate.month;
int month2 = selectedDate.month;
if (month2 > month1) {
age--;
} else if (month1 == month2) {
int day1 = currentDate.day;
int day2 = selectedDate.day;
if (day2 > day1) {
age--;
}
}
return age;
}
print
Text(selectedDate == null ? "" : "$selectedDate"),
Text(selectedDate == null ? "" : "$age"),
From the code you provided I would assume that in setState you should assign ˋage = calAge()ˋ
What is the error in this part of my code that is related to the time in the event calendar app? I'm new to flutter.
this is my code screen :
[Future pickFromDateTime({required bool pickDate}) async {
final date = await pickDateTime(fromDate, pickDate: pickDate);
}
Future<DateTime?> pickDateTime(
DateTime initialDate, {
required bool pickDate,
DateTime? firstDate,
}) async {
if (pickDate) {
final date = await showDatePicker(
context: context,
initialDate: initialDate,
firstDate: firstDate ?? DateTime(2020, 1),
lastDate: DateTime(2101));
if (date == null) return null;
final time =
Duration(hours: initialDate.hour, minutes: initialDate.minute);
return date.add(time);
} else {
final timeOfDay = await showTimePicker(
context: context, initialTime: TimeOfDay.fromDateTime(initialDate));
if (timeOfDay == null) return null;
final Date = DateTime(initialDate.year, initialDate.month);
final Time =
Duration(hours: initialDate.hour, minutes: initialDate.minute);
return date.add(time);
}
}][1]
Careful with your variable names. You declared those variables as ´Date´ and ´Time´, but you are trying to use them by calling ´date´ and ´time´.
Call your variables with lowercase.
final date = DateTime(initialDate.year, initialDate.month);
final time =
Duration(hours: initialDate.hour, minutes: initialDate.minute);
return date.add(time);
My plan is to select 2 dates
start Date
end Date
Where I should get the start date as "20/08/2021 12:00 AM" and end date as "20/08/2021 11:59 PM".
But I am only getting the start Date as "20/08/2021 12:00 AM" not the end Date as "20/08/2021 11:59 PM" , here I am focusing the end Time i.e.,11:59 PM, which is the end time of a day in 12 hour Format, and I am trying to get it by default while picking the end Date.
And for this I already checked the official documentation , I didn't found any resolution.
For reference
https://api.flutter.dev/flutter/intl/DateFormat-class.html
Here is the code
static DateTime nope = DateTime.now();
var selectedStartDate = DateFormat('dd/MM/yyyy hh:mm a');
// var date1 = DateFormat('dd/MM/yyyy').format(nope);
Future<Null> _selectDate(BuildContext context) async {
final DateTime picked = await showDatePicker(
context: context,
initialDate: nope,
firstDate: DateTime(2000, 8),
lastDate: DateTime(2101));
if (picked != null && picked != nope)
setState(() {
nope = picked;
});
startDate.text = selectedStartDate.format(nope);
}
static DateTime yep = DateTime.now();
var selectEndDate = DateFormat('dd/MM/yyyy');
var date2 = DateFormat('dd/MM/yyyy').format(yep);
Future<Null> _selecteddate(BuildContext context) async {
final DateTime pick = await showDatePicker(
context: context,
initialDate: yep,
firstDate: DateTime(2000, 8),
lastDate: DateTime(2101));
if (pick != null && pick != yep)
setState(() {
yep = pick;
});
endDate.text = selectEndDate.format(yep);
}
Here is the output, how I am getting from the above code
Please do help me in searching the solution and Thanks in advance 😀.
Maybe replace
var selectEndDate = DateFormat('dd/MM/yyyy');
with
var selectEndDate = DateFormat('dd/MM/yyyy hh:mm a');
I'm building a POS project now, So I had the same Start Date and End Date Fields in my Tax and other Reports sections. When I faced the same issue I fixed it by adding 23 hours and 59 minutes to the selected date.
I use the below Class to pick a date throughout my application
class DateTimeUtils {
//========== Singleton Instance ==========
DateTimeUtils._internal();
static DateTimeUtils instance = DateTimeUtils._internal();
factory DateTimeUtils() {
return instance;
}
//========== Date Picker ==========
Future<DateTime?> datePicker(BuildContext context, {final DateTime? initDate, final bool endDate = false}) async {
final DateTime? selectedDate = await showDatePicker(
context: context,
initialDate: initDate ?? DateTime.now(),
firstDate: DateTime(1998, 04, 14),
lastDate: DateTime.now(),
);
if (selectedDate != null && endDate) {
final DateTime updated = selectedDate.add(const Duration(hours: 23, minutes: 59));
return updated;
} else {
return selectedDate;
}
}
}
Can be called from anywhere in the project like below
final _selectedDate = await DateTimeUtils.instance.datePicker(context, initDate: toDate, endDate: true);
I have a date and time picker:
Future<void> _showDatePicker(BuildContext context) async {
final picked = await showDatePicker(
context: context,
initialDate: _fromDate,
firstDate: DateTime(1000, 1),
lastDate: DateTime(2200),
);
print(picked);
if (picked != null && picked != _fromDate) {
setState(() {
_fromDate = picked;
});
}
}
Future<void> _showTimePicker(BuildContext context) async {
final picked = await showTimePicker(
context: context,
initialTime: _fromTime,
);
print(picked);
if (picked != null && picked != _fromTime) {
setState(() {
_fromTime = picked;
});
}
}
When I pick a date, the result is 2021-03-10 00:00:00.000 and when I pick a time, the result is TimeOfDay(22:12).
How do I combine both of them to have 2021-03-10 22:12:00.000. That's what I want to save to my database.
And how do I remove the three trailing zeros from the date 2021-03-10 00:00:00.000 of so it can look like this --> 2021-03-10 00:00:00 when displaying it in a Text() widget?
After selecting the date and time, you must generate a new datetime object in this way.
DateTime _fromDate = new DateTime.now();
TimeOfDay _fromTime = new TimeOfDay.now();
DateTime newDateTime = new DateTime(_fromDate.year, _fromDate.month,
_fromDate.day, _fromTime.hour, _fromTime.minute);
Then use the intl package to format the date to the string you want.
String dateTimeFormated = DateFormat('yyyy-MM-dd H:mm:ss').format(newDateTime);
print(dateTimeFormated);
Result will be a string similar to this
2021-03-10 22:12:00