Skip to content

Instantly share code, notes, and snippets.

@Jahidul007
Last active December 26, 2021 11:45
Show Gist options
  • Save Jahidul007/a2dc80fdac4b7fd5b098b48dbd7afbf9 to your computer and use it in GitHub Desktop.
Save Jahidul007/a2dc80fdac4b7fd5b098b48dbd7afbf9 to your computer and use it in GitHub Desktop.
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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment