Formatting Timestamp from Cloud Firestore to Date in Flutter - 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

Related

how i can change Datetime format to (dd.mm.yyyy) in Netjs Prisma

i created db rows in schema.prisma file like below
model Home {
id Int #id #default(autoincrement())
meal_date DateTime #default(now()) #db.Date
meal_day String
meal Meal[]
}
but meal_date is looking very weird like below
2022-08-08T00:00:00.000Z
How i can change this format?
The format of DateTime fields is not configurable. Prisma uses the ISO 8601 standard.

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 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;
}

Flutter - TimeOfDay to Firestore timestamp then into DateTime?

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?

How to assign current date to a date field in odoo 10

How to show current date before clicking the date field in odoo?
Odoo Date field class provides methods to get default values for like today.
For dates the method is called context_today() and for datetimes context_timestamp(). You are able to pass a timestamp to this methods to either get today/now (without timestamp) or a timestamp which will be formed by the logged in users timezone.
Code Example:
from odoo import fields, models
class MyModel(models.Model):
_name = 'my.model'
def _default_my_date(self):
return fields.Date.context_today(self)
my_date = fields.Date(string='My Date', default=_default_my_date)
Or the lambda version:
my_date = fields.Date(
string='My Date', default=lambda s: fields.Date.context_today(s))
I found it.It is Simple, just write this on your python code like:
date = fields.Datetime(string="Date", default=lambda *a: datetime.now(),required=True)
or
like this
date = fields.Datetime(string="Date current action", default=lambda *a: datetime.now())
or
like this
date = fields.Date(default=fields.Date.today)