Flutter - TimeOfDay to Firestore timestamp then into DateTime? - flutter

I have two fields of type TimeOfDay in a model class, which is called startTime and endTime, how do i store this type into a firestore field, as a Timestamp somehow? What i want to achieve is to pick a time like start: 12:00 , end: 13:00 from a timepicker then store it to firestore, and then when reading from firestore i need the time to be of type DateTime, in the format of "12:00" in order to work with this in the weekday calendar widget where the start and end time properties is of type DateTime. Is this possible?

Related

how to convert a time eg- 22:00:00 to a timestamp + include next day date in 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?

Formatting Timestamp from Cloud Firestore to Date in Flutter

I have 2 fields: created_at and updated_at field in my DB on Cloud Firestore. They are of type Timestamp.
In my Model, I have also created the respective fields like so
class User {
String id;
String firstName;
String lastName;
Timestamp created_at;
Timestamp updated_at;
}
I have the respective fromMap and toMap functions in my models. In my view user screen, how do I format my created_at and updated_at fields and display them in Text widgets as 8 May, 2020?
Thanks.
You can convert a Timestamp to a Date like so:
DateTime date = created_at.toDate();
And if you want to display it as a text you can convert your date like this:
String dateString = DateFormat('dd MMM, yyyy').foramt(date);
Therefore you need the intl Package: https://pub.dev/packages/intl

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

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();

Timestamp to LocalDateTime conversion

I am reading a date from Firestore which is of type Timestamp and I want it converted as a LocalDateTime type.
To do so, I used the following procedure:
Convert the Timestamp to a DateTime
Use the .dateTime method of LocalDateTime to convert it to a LocalDateTime
Manually adjust it to my local time
LocalDateTime.dateTime(entity.start.toDate()).addHours(2),
Although entity.start.toDate() has my local time the .dateTime does some adjustments and I get some other time.
Also, this method is prone to errors sinve I am adjusting something manually.
Another way to do so would be the following but I find it too long:
DateTime hStartDate = entity.start.toDate();
LocalDateTime(hStartDate.year,hStartDate.month,hStartDate.day,hStartDate.hour,hStartDate.minute,0)
Any suggestions?
I had a similar issues I wasnt able to find a way to convert Timestamp String to Timestamp object again.
So i used this way out.
When you save data to firestore:
Use -
DateTime.now().toString()
Example :
await Firestore.instance
.collection("users/$docId/tokens")
.document(fcm.deviceToken)
.setData({
"token": fcm.deviceToken,
"timestamp": DateTime.now().toString()
});
When u get data from firestore and get the timestamp string:
Use this to get DateTime object -
DateTime dateTime = DateTime.parse(timestamp)
Use this to get TimeOfDay object -
TimeOfDay timeOfDay = TimeOfDay.fromDateTime(dateTime);
timeOfDay.format(context);
You can achieve this by using toLocal ( ) method.
something like this.
_getDate(//timestamp, "yyyy.dd.MM, HH:mm");
String _getDate(int timestamp, String dateFormat) {
DateTime date = DateTime.fromMillisecondsSinceEpoch(
timestamp * 1000,
).toLocal();
String formattedDateTime = DateFormat(dateFormat).format(date);
return formattedDateTime;
}