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

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

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");
//tz.UTC
Or
tz.TZDateTime.from(DateTime(2021,12,26,03,07), tz.local);
//tz.UTC

Try out the below code
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();
}

Related

How to get the next near future value to the current time in a list in Flutter?

I have a store schedule that comes from the server. And I get the current time. I need to find the near future start_time to my current time in the list I am getting. For example, if the current time is 3:00 pm, I need to get the closest start_time, which is 5:00 pm. Tell me how to do it?
here I am accessing the key 'mon'
String dateFormat = DateFormat('EEE').format(timeNow).toLowerCase();
shopSchedule.templateFull![dateFormat]
You can do this to get closest time after now from your mon list:
String? selectedTime;
for (var element in mon) {
if (selectedTime == null) {
var now = DateTime.now();
DateTime tempDate = DateFormat("yyyy-MM-dd hh:mm").parse(
"${now.year}-${now.month}-${now.day} ${element["start_time"] as String}");
if (tempDate.isAfter(now)) {
selectedTime = element["start_time"];
}
} else {
var now = DateTime.now();
DateTime selectedDate = DateFormat("yyyy-MM-dd hh:mm")
.parse("${now.year}-${now.month}-${now.day} $selectedTime");
DateTime tempDate = DateFormat("yyyy-MM-dd hh:mm").parse(
"${now.year}-${now.month}-${now.day} ${element["start_time"] as String}");
if (tempDate.isBefore(selectedDate) && tempDate.isAfter(now)) {
selectedTime = element["start_time"];
}
}
}

How to convert a simple string to a DateTime object?

I am tring to convert a simple string into a date time but it is showing invalid exception. Here's the code :
void main() {
String date = '12';
date = DateTime.parse(date).toString();
print(date);
}
Can anyone help ?
Your date string is not valid.
Here is example of it:
String time = '2022-01-13';
DateTime parseDate = DateFormat("yyyy-MM-dd").parse(time); // to be date
var inputDate = DateTime.parse(parseDate.toString()); // to be string
var outputFormat = DateFormat('dd MMM yyyy'); // set format be for ex 13 Jan 2022
var outputDate = outputFormat.format(inputDate); // set to be a string
While it doesn't make much sense in this form, it will work:
import 'package:intl/intl.dart';
void main() {
String date = '12';
DateTime dateTime = DateFormat("MM").parse(date);
print(dateTime);
}

Can't convert a Datetime from a string to another format (eg: 14/12/2021 03:34:03 PM to 03:34 pm)

I'am trying to convert a datetime string from one format to another. i tried to use intl package. But i don't know how to convert this string to another format.
The datetime string i'am getting from api is 14/12/2021 03:34:03 PM. I want to show it like this in my app (only time) 03:34 pm (Also want to make PM Small letter).
Try below code hope its helpful to you. I have tried it without using any third party library
String yourDate = '14/12/2021 03:34:03 PM';
DateFormat formateDate = DateFormat('dd/MM/yyyy hh:mm:ss a');
DateTime inputDate = formateDate.parse(yourDate);
String resultDate = DateFormat('hh:mm a').format(inputDate);
print(resultDate.toLowerCase());
Your Widget:
Text(
'Time : ${resultDate.toLowerCase()}',
style: TextStyle(
fontSize: 15,
),
),
Your Result Screen->
If you don't mind doing it the regexp way
import 'package:intl/intl.dart';
void main() {
var s = "14/12/2021 12:34:03 PM";
print(formatDateTimeString(s));
}
String formatDateTimeString(String s) {
RegExp regexp = RegExp("(AM|PM)");
// finds either AM or PM in string and converts it to lowercase
RegExpMatch match = regexp.firstMatch(s)!;
String end = match.group(0)!.toLowerCase();
// change datetime format to HH:mm as desired
DateTime dt = DateFormat("d/MM/yyyy HH:mm:ss").parse(s);
DateFormat newFormat = DateFormat("HH:mm");
String formattedS = newFormat.format(dt);
// append lowercase ampm
formattedS += " " + end;
return formattedS;
}
You can get it by using below,
dateFormat() {
String dateStart = '14/12/2021 03:34:03 PM';
DateFormat inputFormat = DateFormat("dd/MM/yyyy hh:mm:ss a");
DateTime input = inputFormat.parse(dateStart);
String dateOutput = DateFormat("hh:mm a").format(input);
return dateOutput.toLowerCase();
}
String formattedDate = DateFormat('yyyy-MM-dd – kk:mm').format(now);
How to format DateTime in Flutter
please check out, it's for all formated datetime , you just pass your current and required date format
import 'package:intl/intl.dart';
void main() {
var data = "14/12/2021 03:34:03 PM";
print(getFormattedDateFromFormattedString(
value: data,
currentFormat: "yyyy/MM/dd HH:mm:ss a",
desiredFormat: "hh:mm a").toLowerCase()); //03:34 pm
}
String getFormattedDateFromFormattedString(
{required value,
required String currentFormat,
required String desiredFormat,
isUtc = false}) {
String formattedDate = "";
if (value != null || value.isNotEmpty) {
try {
DateTime dateTime =
DateFormat(currentFormat).parse(value, isUtc).toLocal();
formattedDate = DateFormat(desiredFormat).format(dateTime);
} catch (e) {
print("$e");
}
}
// print("Formatted date time: $formattedDate");
return formattedDate;
}

Flutter DateFormat Invalid date format

I have a string like this String dt = '04-09-2021 - 15:00'
i want to convert it into local time zone
and my code is:
if(dt != 'Not Available'){
return DateFormat('dd-MM-yyyy - HH:mm')
.format(DateTime.parse(dt).toLocal())
.toString();
}else{
return dt;
}
But i get the error: Invalid date format 04-09-2021 - 15:00
You should use parse, not format, the format functions receive a date, but you don't have a date, you have an string, use:
DateFormat('dd-MM-yyyy - HH:mm').parse(dt);
Here I wrote a dateformatter and convert it to local time. As well as you can format like as you wish.
// call getFormattedDateFromFormattedString function in Text
Center(
child: Text(getFormattedDateFromFormattedString(
currentFormat: "dd-MM-yyyy - HH:mm",
desiredFormat: "dd MMM yyyy HH:mm a",
value: "04-09-2000 - 15:00")),
)
// date fomatter function
String getFormattedDateFromFormattedString(
{#required String currentFormat,
#required String desiredFormat,
String value}) {
String formattedDate = "";
if (value != null || value.isNotEmpty) {
try {
DateTime dateTime = DateFormat(currentFormat).parse(value, true).toLocal();
formattedDate = DateFormat(desiredFormat).format(dateTime);
} catch (e) {
print("$e");
}
}
// print("Formatted date time: $formattedDate");
return formattedDate.toString();
}
Output:
N.B: My time zone GMT+6.0

convent and find The difference between date time flutter

I need to help with my fluter code,
I have an API its response me a date as a String
{"time": "12/04/2020 16:09:33"}
and I get the current time in my code from the phone using
var now = new DateTime.now();
how I can calculate the difference between two date-time???
You can use the intl library.
import 'package:intl/intl.dart';
String formatDuration(Duration duration) {
return duration.toString().split('.').first.padLeft(8, '0');
}
final s = "12/04/2020 16:09:33";
final formatter = DateFormat('dd/MM/yyyy HH:mm:ss');
final dateTime = formatter.parse(s);
var now = DateTime.now();
var difference = now.difference(dateTime);
print(formatDuration(difference));
print result ex. 53:37:11
You can achieve that using difference() method which accepts date as DateTime object. Hence, first you need to convert your input date which is in String format, into DateTime. Working code below:
String date = "12/04/2020 16:09:33";
DateFormat dateFormat = DateFormat("yyyy/MM/dd HH:mm:ss");
DateTime dateTime = dateFormat.parse(date); // converts into Datetime
var nowDate = DateTime.now();
var difference = nowDate.difference(dateTime);
print(difference); // 17553602:53:49.047936