From a6f8d6fb633265cc29add864cf3a719fa0945f76 Mon Sep 17 00:00:00 2001 From: Paul Cappadona Date: Wed, 20 Apr 2022 12:52:45 +0800 Subject: [PATCH] Fixed issues parsing and returning datetime value from DateTimePickerDialog, which was causing parse errors and incorrect values being returning the the widget onChangedHandler --- lib/date_time_picker.dart | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/date_time_picker.dart b/lib/date_time_picker.dart index 0ea0dfc..2201a01 100644 --- a/lib/date_time_picker.dart +++ b/lib/date_time_picker.dart @@ -896,6 +896,19 @@ class _DateTimePickerState extends FormFieldState { final lsOldValue = _sValue; _sValue = '$_sDate $_sTime'; + // _sValue needs to maintain the true datetime value + // DateTime.tryParse() requires the time component of datetime string in 24 hour format + // but _sValue could be in 12 hour format. So, we need to convert it to 24 hour format. + List hourMinute = _sTime.split(':'); + String minute = int.parse(hourMinute[1]).toString().padLeft(2, '0'); + if (_tTime.period == DayPeriod.pm) { + String hour = ((int.parse(hourMinute[0]) % 12) + 12).toString().padLeft(2, '0'); + _sValue = '$_sDate $hour:$minute'; + } else { + // ensure we don't have a leading 12 hour for AM time + String hour = (int.parse(hourMinute[0]) % 12).toString().padLeft(2, '0'); + _sValue = '$_sDate $hour:$minute'; + } _sValue = _sValue.trim(); if (widget.dateMask != null && widget.dateMask != '') {