Flutter - Calculate difference of two dates in months - date

In Flutter i have two timestamps.
I want to know difference of that. If it´is difference is more than 15 months is incorrect, else is correct.
The code is
DateTime.fromMillisecondsSinceEpoch( from).difference(DateTime.fromMillisecondsSinceEpoch(to)

If timestamp format is in Unix Timestamp, you need convert to DateTime like the code below:
final date1 = DateTime.fromMillisecondsSinceEpoch(1577836800 * 1000).toUtc(); // 01/01/2020 # 12:00am (UTC)
final date2 = DateTime.fromMillisecondsSinceEpoch(1585699200 * 1000).toUtc(); // 04/01/2020 # 12:00am (UTC)
final difference = (date2.difference(date1).inDays / 30).floor();
print(date1);
print(date2);
print(difference);

Edited:
final date1 = DateTime(2019, 10, 12);
final date2 = DateTime.now();
double difference = double.parse(date2.difference(date1).inDays.toString());
if(difference < 456.25){
print("correct");
}else{
print("incorrect");
}

Related

How to add 24 hours to a millisecondsSinceEpoch time in flutter?

I need to add 24 hr on the selected date slot in the calendar in milliseconds since the epoch.
As in the below code if I add 24 hr it will give me 00:00 time for the same day so I tried to use this (23, minutes: 59, seconds: 59) which is wrong.
Please suggest...
DateTime initialTime = selectedDate.value;
DateTime finalTime = selectedDate.value.add(new Duration(hours: 23,minutes: 59,seconds: 59));
int finalTimeInEpoch = finalTime.millisecondsSinceEpoch;
print('This is the initial time for selected day --> $initialTime and This is the final time for selected day --> $finalTime');
int initialTimeForApi = initialTime.millisecondsSinceEpoch;
int finalTimeForApi= finalTimeInEpoch;
print('This is the initial time for selected day --> ${initialTime.millisecondsSinceEpoch} and This is the final time for selected day --> $finalTimeInEpoch in millisecondsSinceEpoch');
Use Like This
DateTime finalTime = selectedDate.value.add(Duration(hours: 24));
if this don fix the issue your problem is selectedDate.value is not a valid format date time

Flutter: get first day of the week and last day of the week?

I would like to make some pagination that holds the records that are on the same week.
So the interval will be between monday 00:00 - sunday 23:59
All this in unix time. So I can query the database the records that are between this interval.
You can use DateTime.
DateTime now = DateTime.now();
int daysOfWeek = now.weekday - 1;
DateTime firstDay = DateTime(now.year, now.month, now.day - daysOfWeek);
DateTime lastDay = firstDay.add(Duration(days: 6, hours: 23, minutes: 59));
print(firstDay);
print(lastDay);
DateTime nextFirst = firstDay.add(Duration(days: 7));
DateTime nextLast = lastDay.add(Duration(days: 7));
print(nextFirst);
print(nextLast);
DateTime prevFirst = firstDay.subtract(Duration(days: 7));
DateTime prevLast = lastDay.subtract(Duration(days: 7));
print(prevFirst);
print(prevLast);
This week first day is
DateTime firstDayOfCurrentWeek() {
DateTime now = DateTime.now();
DateTime firstDayOfCurrentWeek =
now.subtract(Duration(days: now.weekday - 1));
return firstDayOfCurrentWeek.clearTime();
}
DateTime clearTime() {
return DateTime(this.year, this.month, this.day, 0, 0, 0, 0, 0);
}

Check for time interval flutter

I intend running a query if the user's time is between 10am and 4pm. I know I can get current dateTime with DateTime.now() in flutter. How do i get to know if the user's time interval is between say 10pm to 4pm.
You can use DateTime.isAfter, DateTime.isBefore, or DateTime.compareTo to compare DateTime objects.
However, personally I'd use the DateTime extensions from package:basics which makes code more readable than using .compareTo, and unlike .isBefore/.isAfter, makes it easier to have inclusive endpoints for your time interval:
import 'package:basics/date_time_basics.dart';
void main() {
var now = DateTime.now();
var startTime = DateTime(now.year, now.month, now.day, 22); // 10 PM today
var endTime = DateTime(now.year, now.month, now.day, 16); // 4 PM today
// Note that since your desired time spans midnight, startTime > endTime
// and we use || where we'd normally use &&.
if (now >= startTime || now <= endTime) {
print('Between 10 PM and 4 PM');
} else {
print('Outside of 10 PM and 4 PM');
}
}
(Disclosure: I worked on the DateTime extensions to package:basics.)

Flutter set datetime.now in GMT

can i set datetime.now() in GMT timezone or can i set datetime.now() -7 hours ???
this is the variable
String formattedDate = DateFormat('yyyy-MM-dd HH:mm:ss').format(DateTime.now());
im clueless
You can add or substract from a date time by using Duration.
Date dateA = someDate.subtract(Duration( hours: 6 ));

C# code to find whether today Date lies between or equal to the two other specified date

I have two date as
Date1= 7/28/2014 and Date2=7/31/2014.
I want to check whether today date i.e 7/31/2014 lies between the above mentioned data.
Try this :)
DateTime dat1 = Convert.ToDateTime("7/28/2014");
DateTime dat2 = Convert.ToDateTime("8/1/2014");
DateTime today = Convert.ToDateTime(DateTime.Now.ToString("MM/dd/yyy"));
if (today > dat1 && today < dat2)
{
// between
}
else
{
// lies between
}
Maybe your problem lays in Time part of DateTime...if Date2 is 7/31/2014 0:00:00 and Now is 7/31/2014 11:30:00 then Date2 < Now...consider using DateTime::Date property