How would I format the date returned to be displayed with the day and month eg 23 AUG with my code.
var finaldate;
void callDatePicker() async {
var order = await getDate();
setState(() {
finaldate = order;
});
}
Future<DateTime> getDate() {
return showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2020),
lastDate: DateTime(2025),
Text('$finaldate',
You can use DateFormat from the intl package
DateFormat('dd MMM, yyyy').format(finaldate) // gives 27 Jun, 2020
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);}
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 am trying to give user an option to select date between 7 days.
Like today is 2nd March then user can select any date before 10th March.
My current code is showing first date as 01/01/03 i am not sure why it is giving me this date.
Here is the code.
_pickedDate() async {
rescheduleddate = await showDatePicker(
context: context,
firstDate: DateTime(DateTime.now().day + 1),
lastDate: DateTime(DateTime.now().day + 7),
initialDate: DateTime(DateTime.now().day + 1),
);
if (rescheduleddate != null) {
setState(() {
pickeddate = rescheduleddate;
});
}
}
I am not sure what am i doing wrong because if i add year instead of day then it is working fine.
I managed to solve this one.
_pickedDate() async {
rescheduleddate = await showDatePicker(
context: context,
firstDate: DateTime.now().add(new Duration(days: 1)),
lastDate: DateTime.now().add(new Duration(days: 7)),
initialDate: DateTime.now().add(new Duration(days: 1)),
);
if (rescheduleddate != null) {
setState(() {
pickeddate = rescheduleddate;
});
}
}
I think the issue might be that you aren't properly adding a duration to the current DateTime object. By calling .day you are just getting the number of the day as an integer, which you're then trying to plug back into a new DatePicker as an argument. For example, if today is March 2nd, DateTime.now().day would return 2, so your firstDate value is currently trying to evaluate what DateTime(3) is (if you inspect the DateTime function, the first argument is the year as an integer, so it's setting the date to 01/01/03). Try doing this:
rescheduleddate = await showDatePicker(
context: context,
firstDate: DateTime.now().add(Duration(days: 1)),
lastDate: DateTime.now().add(Duration(days: 7)),
initialDate: DateTime.now().add(Duration(days: 1)),
);
I can get the date time values with date and time picker. I want to combine the date and time values I have obtained and compare them with the current date (DateTime.now() format).
How can I do it?
Future _selectDayAndTime(BuildContext context) async {
DateTime _selectedDay = await showDatePicker(
context: context,
initialDate: _date ?? DateTime.now(),
firstDate: DateTime(2020),
lastDate: DateTime(2100),
builder: (BuildContext context, Widget child) => child
);
TimeOfDay _selectedTime = await showTimePicker(
context: context,
initialTime: _time ?? TimeOfDay.now(),
);
if(_selectedDay != null && _selectedTime != null) {
setState(() {
_date = _selectedDay;
_time = _selectedTime;
});
debugPrint("Day result : $_date");
debugPrint("Time result : $_time");
}
}
Result
I/flutter ( 9596): Day result : 2020-01-16 00:00:00.000
I/flutter ( 9596): Time result : TimeOfDay(18:30)
TimeOfDay holds only hour and minutes. showDatePicker() returns a DateTime object, although it contains only year, month and day which are meaningful. You may update this DateTime object's hour and minutes with that of TimeofDay.
A vague code would as shown below.
var _date = _selectedDay;
var _time = _selectedTime;
var updatedDateTime = new DateTime(_date.year, _date.month, _date.day, _time.hour, _time.minute, _date.second);
And now, you can use standart DateTime class methods, to compare this updatedDateTime with DateTime.now().
This might help you :
var date = DateTime.now();
var time = TimeOfDay(hour: 12, minute: 00);
// Combined DateTime and TimeOfDay
var pickedTime = DateTime(date.year, date.month, date.day, time.hour, time.minute);
// returns -1 if pickedTime is before, 1 if after, 0 if equal.
var comparison = pickedTime.compareTo(date);
// time between now and the time of day.
var duration = Duration(milliseconds: date.millisecondsSinceEpoch - pickedTime.millisecondsSinceEpoch);