如何将 "MM/DD/YYYY" 转换为 TZDateTime 格式

How to convert "MM/DD/YYYY" to TZDateTime format

我正在我的待办事项应用程序中实现 flutter 本地通知插件,我想安排 特定日期和时间的通知,日期选择器和时间选择器显示日期如下:12/26/2021 时间如下:03:17 PM ,如何将其转换为 TZDateTime 格式

试试下面的代码

 TZDateTime tzDateTime;
    String dateTime = getFormattedDateFromFormattedString(
        value: "12/26/2021 3:16 PM",
        currentFormat: "MM/dd/yyyy hh:mm a",
        desiredFormat: "yyyy-MM-dd HH:mm:ss");

    tz.initializeTimeZones();
    tzDateTime = tz.TZDateTime.parse(tz.local, dateTime);

    print(tzDateTime);
}

// format your given time
  getFormattedDateFromFormattedString(
      {value, String currentFormat, String desiredFormat, isUtc = true}) {
    DateTime dateTime = DateTime.now();
    if (value != null || value.isNotEmpty) {
      try {
        dateTime = DateFormat(currentFormat).parse(value, isUtc).toLocal();
      } catch (e) {
        print("$e");
      }
    }
    return dateTime.toString();
  }

导入时区

import 'package:timezone/data/latest_all.dart' as tz;
import 'package:timezone/timezone.dart' as tz;


tz.initializeTimeZones();
tz.TZDateTime.parse(tz.local, "2012-12-26 03:17:00");
//tz.UTC

或者

tz.TZDateTime.from(DateTime(2021,12,26,03,07), tz.local);
//tz.UTC