As the title suggests I am using DateRangePicker to add date ranges to an array. If possible I would like to be able to "grey" out already selected dates in the array. Is there anyway to do this?
Here is the solution to returning the dates in-between the range in case anyone else needs it.
List<DateTime> getDaysInBetweenIncludingStartEndDate(
{required DateTime startDateTime, required DateTime endDateTime}) {
// Converting dates provided to UTC
// So that all things like DST don't affect subtraction and addition on dates
DateTime startDateInUTC =
DateTime.utc(startDateTime.year, startDateTime.month, startDateTime.day);
DateTime endDateInUTC =
DateTime.utc(endDateTime.year, endDateTime.month, endDateTime.day);
// Created a list to hold all dates
List<DateTime> daysInFormat = [];
// Starting a loop with the initial value as the Start Date
// With an increment of 1 day on each loop
// With condition current value of loop is smaller than or same as end date
for (DateTime i = startDateInUTC;
i.isBefore(endDateInUTC) || i.isAtSameMomentAs(endDateInUTC);
i = i.add(const Duration(days: 1))) {
// Converting back UTC date to Local date if it was local before
// Or keeping in UTC format if it was UTC
if (startDateTime.isUtc) {
daysInFormat.add(i);
} else {
daysInFormat.add(DateTime(i.year, i.month, i.day));
}
}
return daysInFormat;
}
Yes, you can send disabled dates into the component.
Check this sample of the documentation.
For further options, check the whole docs.
Related
I've got a list of data which also contains ISO8601 DateTime format. How can I sort it by time not date?
[2022-12-06T22:37:49.292343,
2022-12-06T18:37:49.300045,
2022-12-06T15:37:49.307976]
To sort the list of dates by their hour, you can use:
void main() {
final dates = [
'2022-12-06T22:39:50.292343',
'2022-12-06T18:37:49.300045',
'2022-12-06T15:37:49.307976'
];
dates
.sort((a, b) => DateTime.parse(a).hour.compareTo(DateTime.parse(b).hour));
print(dates);
}
Output:
[2022-12-06T15:37:49.307976, 2022-12-06T18:37:49.300045, 2022-12-06T22:39:50.292343]
Parse the strings to DateTime objects.
If you want to ignore the date and sort only by time, create new DateTime objects that copy the times and that all use the same date.
Sort by those new DateTime objects.
void main() {
var dateTimeStrings = [
'2022-12-01T22:37:49.292343',
'2022-12-02T18:37:49.300045',
'2022-12-03T15:37:49.307976',
];
/// Replaces the date portion of a [DateTime] with a fixed date,
/// retaining the time portion.
///
/// The returned [DateTime] object will be in UTC.
///
/// In Dart 2.19, this implementation can be simplified with the
/// [DateTime.copyWith] extension.
DateTime clobberDate(DateTime dateTime) => DateTime.utc(
2022,
1,
1,
dateTime.hour,
dateTime.minute,
dateTime.second,
dateTime.millisecond,
dateTime.microsecond,
);
dateTimeStrings.sort((a, b) {
var dt1 = clobberDate(DateTime.parse(a));
var dt2 = clobberDate(DateTime.parse(b));
return dt1.compareTo(dt2);
});
dateTimeStrings.forEach(print);
}
which prints:
2022-12-03T15:37:49.307976
2022-12-02T18:37:49.300045
2022-12-01T22:37:49.292343
Note that the above sort callback could be unnecessarily expensive for long lists since it could call DateTime.parse multiple times on the same Strings. It'd be better to convert your List<String> to a List<DateTime> first and then sort that. Or, if you must start and end with Strings, you could use a Schwartzian transform.
I want to ask how to convert a given time for example 22:00:00 into a timestamp and also add the next day date to it while converting into a time stamp in flutter.
Thank You
You can convert a Date string to a timestamp
convertDateTimeToTimestamp(String yourDateTime, [Duration? extraDuration]) {
DateTime date = DateTime.parse(yourDateTime);
if (extraDuration != null) {
date = date.add(extraDuration);
}
return date.microsecondsSinceEpoch;
}
then your example with one additional day (next day) can be:
main() {
final timestamp = convertDateTimeToTimestamp(
"2022-03-05 22:00:00",
Duration(days: 1),
);
print(timestamp); //output: 1646600400000000
// try to check the converted timestamp with addition duration in the example above, it's only one day
DateTime date = DateTime.fromMicrosecondsSinceEpoch(timestamp);
print('${date.year}-${date.month}-${date.day} ${date.hour}:${date.minute}:${date.second}'); //output: 2022-3-6 22:0:0
}
you can use intl package and format your datetime easier.
Timestamp data type is defined in cloud_firestore.
What do you mean by timestamp?
I have a calendar and in the calendar events are added in here
Map<DateTime, List<EventStore>> get events => _events;
where EventStore is anotherClass like this,
class EventStore{
String subject;
String level;
String room;
EventStore({this.subject,this.level,this.room});
}
Now, I want to compare today's date in yyyy-mm-dd format with the calendar date format. And I also don't know how to see the calendar date format.
How do I compare today's date in yyyy-mm-dd format with the calendar date format? So that I can show all the events that are on the particular date anywhere in my app?
And can anybody say, what is the DateTime format in map,
Map<DateTime, List<EventStore>> get events => _events;
DateTime objects don't contain a date format, it's technically a number of elapsed time since 01-01-1970 https://api.flutter.dev/flutter/dart-core/DateTime-class.html
You can compare DateTime objects using their attributes.
DateTime savedDateTime = getSavedDateFromServer();
DateTime now = DateTime.now();
bool isSameYear = savedDateTime.year == now.year;
bool isSameMonth = savedDateTime.month == now.month;
//etc.
If you want to group your objects by date I recommend you take a look at this:
Flutter/Dart how to groupBy list of maps
I want to get a stream of record which are in the same day of input (like all records with date of Feb 23, 2020).
Stream<List<BloodSugar>> watchBloodSugarsInDate(DateTime date) {
return (select(bloodSugarsEntity)
..where((bp) => bp.date.equals(date)))
.map((bloodSugarsEntity) => convertEntityToModel(bloodSugarsEntity))
.watch();
}
this is my code and it doesn't work because dateTime is combine of date and time. I tried using nested queries for separated comparison of year, month and day but I couldn't make it.
Any help would be much appriciated.
You can get date from your row, and just compare with passed searchDate.
You have to compare year with year, month with month and day with day:
Stream<List<BloodSugar>> watchBloodSugarsInDate(DateTime searchDate) {
return (select(bloodSugarsEntity)
..where(
(row) {
final date = row.date;
return date.year.equals(searchDate.year) &
date.month.equals(searchDate.month) &
date.day.equals(searchDate.day);
},
))
.map((bloodSugarsEntity) => convertEntityToModel(bloodSugarsEntity))
.watch();
}
bool isSameDate(bp) {
final value = bp.date;
return value.year == date.year &&
value.month == date.month &&
value.day == date.day;
}
In an input json file i receive dates in this format:
{ "dt_received" : "2016-01-22T12:35:52.123+05" }
When loaded into MongoDB, those dates are stored this way:
dt_received: "2016-01-22T07:35:52.123Z"
The issue is that i need the timezone to calculate my indicator.
In constraint, i can't create new columns such as "dt_received_timezone".
So i'm looking for changing the date storage format into MongoDB in order to make the timezone appear (or at least not disapear)
Is it a way to to this? Or any solution ?
If you receive data from various time zones and want to keep the time zone offset, you will have to save it into the database like this:
var now = new Date();
db.data.save( { date: now, offset: now.getTimezoneOffset() } );
You can then reconstruct the original time like this
var record = db.data.findOne();
var localNow = new Date( record.date.getTime() - ( record.offset * 60000 ) );
See the documentation for further details