1

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

4

2 回答 2

1

导入时区

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
于 2021-12-26T10:13:21.317 回答
0

试试下面的代码

 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();
  }
于 2021-12-26T10:01:39.573 回答