Home > Software design >  How to convert "MM/DD/YYYY" to TZDateTime format
How to convert "MM/DD/YYYY" to TZDateTime format

Time:12-27

I am implementing flutter local notification plugin in my todo app and I want to schedule the notification for a specific day and time, The date picker and time picker shows the date like this: 12/26/2021 and the time like this: 03:17 PM, How do I convert this to TZDateTime format

CodePudding user response:

import timezone

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");

Or

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

CodePudding user response:

You need to implement the Iso8601String. Try out the below code

import 'package:intl/intl.dart';

void main() {
  var data = "12/26/2021 3:16 PM";

  String dateTime = getFormattedDateFromFormattedString(
      value: data,
      currentFormat: "MM/dd/yyyy hh:mm a",
      desiredFormat: "yyyy-MM-ddTHH:mm:ss.mmmuuuZ");
  
  print(dateTime); //2021-12-15T15:16:00.000Z
}

getFormattedDateFromFormattedString(
    {required value,
    required String currentFormat,
    required String desiredFormat,
    isUtc = true}) {
  DateTime? dateTime = DateTime.now();
  if (value != null || value.isNotEmpty) {
    try {
      dateTime = DateFormat(currentFormat).parse(value, isUtc);
    } catch (e) {
      print("$e");
    }
  }
  return dateTime!.toIso8601String();
}

For more about ios8601String

  • Related