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.)
Related
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
I am trying to implement something which requires the date to be set at 00:00:00 for the next day.
For example :
DateTime? created_at = DateTime.now(); //lets say Jul 25 10:35:90
Now I want a variable start_date whose value should be Jul 26 00:00:00
How can I achieve this ?
Any help will be appreciated.
You can do something like below
DateTime? now = DateTime.now(); //lets say Jul 25 10:35:90
var lastMidnight = DateTime(now.year, now.month, now.day + 1);
It return 2022-07-26 00:00:00.000
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);
}
I have converted a Date into DateTime format, and it is returning me the hour format in 00:00:00 but I want it to be in 23:59:59
Date startDate = Date.newInstance(2021,2,1);
This returns the output as 2021-02-01 00:00:00
When I try to convert this to the 23:59:59 hour format by using the below code
DateTime startDateConvertTwo = DateTime.newInstance(startDate, Time.newInstance(23, 59, 59, 0));
It is pushing the date to next day and returning the value of 2021-02-02 07:59:59
I tried to sort this out by changing the values of Time.newInstance by adding it as Time.newInstance(15, 59, 59, 0) by doing which I get the expected result. But is it the right way to achieve what I am trying to do?
Please let me know if there are any other ways.
The returned output of Date startDate = Date.newInstance(2021,2,1); is not 2021-02-01 00:00:00. It's just a date with no information about time, but System.debug() display it as a DateTime, that's why you see 00:00:00.
Try System.debug(String.valueOf(startDate)); to see only the Date part.
DateTime.newInstance(date, time)
Constructs a DateTime from the specified date and time in the local time zone.
As documentation states, the DateTime you get is in your own time zone. Anyway System.debug() shows it in UTC time zone (GMT+0), so if your time zone is GMT-8 you'll see 2021-02-02 07:59:59.
System.debug(String.valueOf(startDateConvertTwo )); will shows the DateTime in your own time zone, so you'll see 2021-02-01 23:59:59.
If you need a DateTime in GMT you could use DateTime.newInstanceGmt(date, time):
DateTime startDateGMT = DateTime.newInstanceGmt(startDate, Time.newInstance(23, 59, 59, 0));
If you cannot use that method, you could add your offset to a DateTime:
public static DateTime toUTC(DateTime value) {
Integer offset = UserInfo.getTimezone().getOffset(value);
return value.addSeconds(offset/1000);
}
You could test it in anonymous console:
Date startDate = Date.newInstance(2021,2,1);
DateTime startDateConvertTwo = DateTime.newInstance(startDate, Time.newInstance(23, 59, 59, 0));
DateTime startDateGMT = DateTime.newInstanceGmt(startDate, Time.newInstance(23, 59, 59, 0));
DateTime startDateGMT2 = toUTC(startDateConvertTwo);
System.debug('startDateConvertTwo: ' + startDateConvertTwo); // startDateConvertTwo: 2021-02-01 22:59:59 // Because I'm at GMT+1
System.debug('String.valueOf(startDateConvertTwo): ' + String.valueOf(startDateConvertTwo)); // String.valueOf(startDateConvertTwo): 2021-02-01 23:59:59
System.debug('startDateGMT: ' + startDateGMT); // startDateGMT: 2021-02-01 23:59:59 // Now it's in UTC
System.debug('String.valueOf(startDateGMT): ' + String.valueOf(startDateGMT)); // String.valueOf(startDateGMT): 2021-02-02 00:59:59 // So in my locale time it's the day after,
System.debug('startDateGMT2: ' + startDateGMT2); // startDateGMT2: 2021-02-01 23:59:59 // Same as startDateGMT
System.debug('String.valueOf(startDateGMT2): ' + String.valueOf(startDateGMT2)); // String.valueOf(startDateGMT2): 2021-02-02 00:59:59
public static DateTime toUTC(DateTime value) {
Integer offset = UserInfo.getTimezone().getOffset(value);
return value.addSeconds(offset/1000);
}
The output of startDateGMT and startDateGMT2 will be the same.
Noteworthy: DateTime fields are stored in GMT. When shown in the standard Salesforce UI, they're converted to the user's timezone.
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");
}