how to convert a time eg- 22:00:00 to a timestamp + include next day date in Flutter - flutter

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?

Related

DateRangePicker - Grey out unavailable dates

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.

How to get particular month starting date of current year in flutter

how to get current year starting date or particular month starting date programmatically in flutter
You can find first month with the first day (1st January) of the current year easily by doing this
DateTime(DateTime.now().year) //specify only current year
by doing this you will get a date as
2022-01-01 00:00:00.000
import 'package:intl/intl.dart';
main() {
static final DateTime now = DateTime.now();
static final DateFormat formatter = DateFormat('yyyy-MM-dd');
final String formatted = formatter.format(now);
print(formatted); // something like 2022-04-20
}

Comparing calendar datetime to yyyy-mm-dd format in flutter

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

Timestamp from Firebase to String date

I have a Timestamp model and get a Timestamp from Firebase.
print(user.endDate.toDate()); = flutter: 2020-08-05 00:00:01.000
A newbe question, but I'm struggling with what I should user here: Text(?) to get the date
use the time class
example:
//before we show the date we need to parse it then show it in a format Month 2 , 2020
Container(
child: Text(new DateFormat.yMMMd().format(DateTime.parse(productInfo["createdAt"]))),
),
// this is your time => productInfo["createdAt"]
DateTime class
Timestamp firestoreTimestamp = data['timestamp'];
DateTime dateTime = firestoreTimestamp.toDate();
Text(dateTime.toString());

Flutter Firstore Timestamp now / current date

Below are all the Firebase timestamp methods that I have used and found handy.
How to get a Firestore timestamp (Todays date)
Timestamp timestampDate = Timestamp.now();
How to set a Timestamp from Datetime
DateTime someDate = DateTime.now();
Timestamp timestampDate = Timestamp.fromDate(DateTime(someDate.year, someDate.month, someDate.day))
How to get the Date object from Timestamp
DateTime dateTime = timestampDate.toDate();