I want to apply formatting to a time that the TimePicker gives me, since it gives me 13:30, but when I want to print it I get 1:30 PM, and I want it to give me 13:30 only.
I attach the code of the _pickTime:
//Función que muestra el Time Picker.
_pickTime() async {
TimeOfDay? timeRecord = await showTimePicker(
context: context,
initialTime: time.replacing(hour: time.hourOfPeriod),
);
if (timeRecord != null) {
setState(() {
finaltime = time.format(context);
});
}
}
use intl package
var df = DateFormat("h:mm a");
var dt = df.parse(timeRecord!.format(context));
print(DateFormat('HH:mm').format(dt));
so your function would look like
_pickTime() async {
TimeOfDay? timeRecord = await showTimePicker(
context: context,
initialTime: time.replacing(hour: time.hourOfPeriod),
);
if (timeRecord != null) {
setState(() {
var df = DateFormat("h:mm a");
var dt = df.parse(timeRecord!.format(context));
var finaltime = DateFormat('HH:mm').format(dt);
//print(finaltime)
// this will return 13:30 only
});
}
}
You can use intl package.
And use this before you print
DateFormat.jm().format(paste your yourdatetime here)
Try this with intl.
final dateNow = DateTime.now();
final date = DateTime(dateNow.year, dateNow.month, dateNow.day, finalTime.hour,
finalTime.minute);
final timeStringFormatted = DateFormat(DateFormat.HOUR24_MINUTE)
.format(date);
Related
I always get this error
Trying to read from 08:43 at position 6 whenever am converting from TimeOfDay to String using _pickedTime.format(context). It throws the error on my real device but works perfectly on my emulator. How do I solve this error. Below is more codes fo review:
DateTime date = DateFormat.jm().parse(task.startTime.toString());
var myTime = DateFormat("HH:mm").format(date);
notificationService.scheduleNotification(
hour: int.parse(myTime.toString().split(":")[0]),
minutes: int.parse(myTime.toString().split(":")[1]),
task: task
Then on my time conversion this:
String _endTime = '9:30PM';
String _startTime = DateFormat('hh:mm a').format(DateTime.now()).toString();
_getTimeFromUser({required bool isStartTime}) async {
var pickedTime = await _showTimePicker();
String formattedTime = pickedTime.format(context);
if(isStartTime == true) {
setState(() {
_startTime = formattedTime;
});
}else if(isStartTime == false) {
setState(() {
_endTime = formattedTime;
});
}else if(pickedTime == null) {
debugPrint('Time Cancelled');}
}
_showTimePicker() {
return showTimePicker(context: context,
initialEntryMode: TimePickerEntryMode.input,
initialTime: TimeOfDay(
hour: int.parse(_startTime.split(':')[0]),
minute: int.parse(_startTime.split(':')[1].split(' ')[0])));
}
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);
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
I want to format the time I get from the timePicker, but I get an error. The variable t (String formatedTime = new DateFormat('HH:mm').format(t);) is red underlined when I try to format the time and I don't know why.
This is the code I tried:
pickTime() async {
TimeOfDay t = await showTimePicker(
context: context,
initialTime: pickedTime,
);
if (t != null) {
setState(() {
pickedTime = t;
print(pickedTime.toString());
String formatedTime = new DateFormat('HH:mm').format(t);
print(formatedTime.toString());
});
}
}
I'm getting time using showTimePicker().
TimeOfDay _selectedTime;
Future<Null> _selectTime(BuildContext context) async {
final TimeOfDay timePicked = await showTimePicker(
context: context,
initialTime: TimeOfDay.now(),
);
if (timePicked != null)
setState(() {
_selectedTime = timePicked;
});
}
But here _selectedTime is in 24-hour format. Suppose if I select 3.00 PM then it shows 15.00 i.e. it's in 24-hour format.
How can I convert this time into 12-hour format? i.e It should show me 3.00 PM and not 15.00.
I can write the logic to convert it. But is there anything built in to do so?
You can use hourOfPeriod property of TimeofDay to return to you the hour in 12-hour format. That, of course, will be without the AM - PM ending :
TimeOfDay _selectedTime;
Future<Null> _selectTime(BuildContext context) async {
final TimeOfDay timePicked = await showTimePicker(
context: context,
initialTime: TimeOfDay.now(),
);
if (timePicked != null)
setState(() {
_selectedTime = timePicked.replacing(hour: timePicked.hourOfPeriod);
});
print(_selectedTime.toString());
}
Use DateFormat to convert time to 12 hours format with AM & PM
TimeOfDay? _selectedTime;
Future<Null> _selectTime(BuildContext context) async {
final TimeOfDay? timePicked = await showTimePicker(
context: context,
initialTime: TimeOfDay.now(),
);
if (timePicked != null)
setState(() {
_selectedTime = timePicked;
});
// Conversion logic starts here
DateTime tempDate = DateFormat("hh:mm").parse(
_selectedTime!.hour.toString() +
":" + _selectedTime!.minute.toString());
var dateFormat = DateFormat("h:mm a"); // you can change the format here
print(dateFormat.format(tempDate));
}
Output:
flutter: 8:00 PM
flutter: 1:00 AM
flutter: 6:00 PM
I have created a extension for parsing TimeOfDay to 12 hr format
extension TimeOfDayExtensions on TimeOfDay {
String format12Hour(BuildContext context) {
TimeOfDay time = replacing(hour: this.hourOfPeriod);
MaterialLocalizations localizations = MaterialLocalizations.of(context);
final StringBuffer buffer = StringBuffer();
buffer
..write(time.format(context))
..write(' ')
..write(period == DayPeriod.am
? localizations.anteMeridiemAbbreviation
: localizations.postMeridiemAbbreviation);
return '$buffer';
}
You can use like
pickedTime?.format12Hour(context);
i used jiffy to get 12 hour format and the am/pm
_selectTime(BuildContext context) async {
final TimeOfDay? timeOfDay = await showTimePicker(
context: context,
initialTime: TimeOfDay.now(),
initialEntryMode: TimePickerEntryMode.input,
hourLabelText: LocalizationKeys.hour.tr,
minuteLabelText: LocalizationKeys.minute.tr,
);
if (timeOfDay != null) {
DateTime dateTime = DateTime(DateTime.now().year, DateTime.now().month,
DateTime.now().day, timeOfDay.hour, timeOfDay.minute);
var jiffy = Jiffy(dateTime);
var format = jiffy.format("hh:mm a");
_lecturesViewModel.selectedTime.value = format;
}