String oldDateStr = "2022-08-1";
String oldTimeStr = "03:10 pm";
DateTime? newDateTime; //2022-08-01 13:10 (yyyy-MM-dd HH:mm) <----- TARGET
final olddateFormat = DateFormat("yyyy-MM-d");
final newdateFormat = DateFormat("yyyy-MM-dd");
final oldtimeFormat = DateFormat("hh:mm a");
final timeFormatter = DateFormat("HH:mm");
DateTime oldTime12 = oldtimeFormat.parse(oldTimeStr.toUpperCase());
DateTime newTime24 = DateTime.parse(timeFormatter.format(oldTime12)); //This line is culprit in print() debug
DateTime oldDate = olddateFormat.parse(oldDateStr);
DateTime newDate = DateTime.parse(newdateFormat.format(oldDate));
I tried & combined various answers from SO posts but couldn't figure this out. So I'm posting the question by helplessness.
CodePudding user response:
The accepted format is yyyy-MM-dd hh:mm aaa
. am/pm marker assumes capital letters e.g PM.
import 'package:intl/intl.dart';
void main() {
String oldDateStr = "2022-08-1";
String oldTimeStr = "03:10 pm";
final result = DateFormat("yyyy-MM-dd hh:mm aaa").parse(
'$oldDateStr $oldTimeStr'.replaceAll('pm', 'PM').replaceAll('am', 'AM'),
);
print(result); // 2022-08-01 15:10:00.000
}
See DateFormat for more information.
CodePudding user response:
use this package to combine date and time and convert your datetime format to DateTime
String oldDateStr = "2022-08-1";
String oldTimeStr = "03:10 pm";
final String dateTimeString = oldDateStr " " oldTimeStr;
final DateFormat format = new DateFormat("yyyy-MM-dd hh:mm a");
print (format.parse(dateTimeString));
CodePudding user response:
You can get formatted date like this, make a function if you are using it frequently.
String oldDateStr = "2022-08-01";
String oldTimeStr = "03:10 pm";
DateTime? newDateTime;
String newDate = (oldDateStr " " oldTimeStr.substring(0, 5) ":00.000");
DateFormat formatter = DateFormat ('yyyy-MM-dd hh:mm');
DateTime nt = DateTime.parse(newDate);
print(formatter.format(nt));