How do i compare current timestamp to the timestap from firebase flutter - flutter

I want to create a function which does not allow the user to remove the appointment once the timestamp for the appointment has past already. But what i tried below does not work, i hope to get some guidance from you guys
My widget.filter is a var which has the timestamp value from firebase
DateTime currentPhoneDate = DateTime.now(); //DateTime
Timestamp myTimeStamp = Timestamp.fromDate(currentPhoneDate); //To TimeStamp
DateTime myDateTime = myTimeStamp.toDate(); // TimeStamp to DateTime
print("current phone data is: $currentPhoneDate");
print("current phone data is: $myDateTime");
if(myTimeStamp < widget.filter){
print('work');
}else{
print('fail');
}

DateTime currentPhoneDate = DateTime.now();
Timestamp myTimeStamp = Timestamp.fromDate(currentPhoneDate);
DateTime myDateTime = myTimeStamp.toDate();
print("myTimeStamp is: $myTimeStamp");
print("currentPhoneDate is: $currentPhoneDate");
print("myDateTime is: $myDateTime");
// if widget.filter DataType is not Timestamp then first convert it to Timestamp.
if (myTimeStamp.millisecondsSinceEpoch <
widget.filter.millisecondsSinceEpoch) {
print('work');
} else {
print('fail');
}

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 calculate working hours from api data in flutter

i have fetched data from an api which contains employees working time,
i want to calculate total working hours each day
here's how i get the data from the api for 1 single day
Future<List> getPunchData(String empCode, DateTime date) async {
String ip = await confObj.readIp();
DateTime end = new DateTime(date.year, date.month, date.day, 23,59,59);
final response = await http.get(Uri.parse("url/$empCode&$date&$end" ));
final String t = response.body;
var jsonData =jsonDecode(t);
return jsonData;
}
the api result is this:
{
"id": 10,
"punch_time": "2022-03-08 13:30:19.000000",
},
{
"id": 11,
"punch_time": "2022-03-08 16:22:39.000000",
}..
..
..
how can i automatically calculate and isplay total hours when after the widget is loaded
You can use the parse function of the DateTime object to convert the String date into DateTime.
The code would somewhat look like this (can't say for sure as I don't know your API):
final DateTime startTime = DateTime.parse(jsonData[0]['punch_time']);
final DateTime endTime = DateTime.parse(jsonData[1]['punch_time']);
Once you have the DateTime object, you can use the difference function to get a Duration object which will tell you the hours an employee has worked.
final Duration durationWorked = startTime.difference(endTime);
final int hoursWorked = durationWorked.inHours;

flutter:: Is it possible to change the timezone of a datetime?

I want to represent the time the file was saved as a string. The time in my country is 9 hours ahead of utc time. How can I change the current utc time to 9 hours faster?
String _getTime({required String filePath}) {
String fromPath = filePath.substring(
filePath.lastIndexOf('/') + 1, filePath.lastIndexOf('.'));
if (fromPath.startsWith("1", 0)) {
DateTime dateTime =
DateTime.fromMillisecondsSinceEpoch(int.parse(fromPath));
var dateLocal = dateTime.toLocal();
print(dateLocal);
print(dateTime);
int year = dateLocal.year;
int month = dateLocal.month;
int day = dateLocal.day;
int hour = dateLocal.hour;
int min = dateLocal.minute;
String dato = '$year-$month-$day--$hour:$min';
return dato;
} else {
return "No Date";
}
}
Use this package ---->>>>> https://pub.dev/packages/flutter_native_timezone
Add the package dependencies to your project, import the package into the file you're working in and write the code below to get your currenTimeZone
final String currentTimeZone = await FlutterNativeTimezone.getLocalTimezone();
debugPrint(currentTimeZone);

How to implement Upcoming Date Countdown in flutter

I have a task to implement upcoming event date countdown. Example 1 months 2 days remaining. date is received from api like this 2021-10-18 17:00:00.000Z. I tried some examples but didn't got required output. Below is my implemented code
static String? dateDiff(String date){
if(date != "null") {
final date1 = DateTime.now().toUtc();
final DateTime date2 = DateTime.parse(date).toUtc();
final difference = date2.difference(date1).inDays;
print(difference); //
return difference.toString(); //
}
}
You can check the answer here out
Check here

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