Storing Timestamp in firebas FireStore

2,253 views
Skip to first unread message

sarfaraz ahmed

unread,
Nov 8, 2020, 4:44:14 PM11/8/20
to Flutter Development (flutter-dev)
Hello Guys,

I am new to this group. I am trying to store TimeStamp to Firestore. Now this timestamp is value pickedup from Datepicker. 

So, my model is like User {name, dateofbirth}

Also, I am using JSON Serializer to encode and decode models from and toJSon

What I understood so far is I can store this date as milli second from epoch as number is firebase or as Datetime as String. 
Whereas Timestamp is only storing serverside timestamp. 

If anyone has an idea, please share.

Regards
Sarfaraz

Suzuki Tomohiro

unread,
Nov 8, 2020, 7:32:54 PM11/8/20
to sarfaraz ahmed, Flutter Development (flutter-dev)
Timestamp is only storing serverside timestamp

This is not true. It’s just often used with serverside timestamp (FieldValue.serverTimestamp). You can save your specifying time in the type.

Also, it’s a bad idea to use milliseconds-since-epoch (or Timestamp) for date of birth, because we have time zones. Think of a person is born in Japan (UTC+9) and currently lives in New York (UTC-5).

Regards,
Tomo


--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/21ea10c0-0112-4372-8c28-ce4e0e39a025n%40googlegroups.com.

sarfaraz ahmed

unread,
Nov 9, 2020, 12:34:24 AM11/9/20
to Flutter Development (flutter-dev)
Thank you for reply. It took me one step closer to solution.  Here is my model and serialization code below. it stores the dateofbirth as string. 

@JsonSerializable(nullable: false)
class UserProfileModel {
  String aboutMe;

  @JsonKey(fromJson: _fromJson, toJson: _toJson)
  DateTime dateofbirth;
  String business;
  String gender;
  String city;
  String pin;
  String state;

  UserProfileModel(
      {this.aboutMe,
      this.dateofbirth,
      this.business,
      this.gender,

      this.city,
      this.pin,
      this.state});

  factory UserProfileModel.fromJson(Map<String, dynamic> data) =>
      _$UserProfileModelFromJson(data);

  Map<String, dynamic> toJson() => _$UserProfileModelToJson(this);

  static DateTime _fromJson(String String) => DateTime.tryParse(String);
      
      
  static String _toJson(DateTime time) => time.toIso8601String();
}

I want to save dateofbirth as timestamp. 

Regards,
Sarfaraz Ahmed

Suzuki Tomohiro

unread,
Nov 9, 2020, 7:59:35 AM11/9/20
to sarfaraz ahmed, Flutter Development (flutter-dev)
You don’t want to use Timestamp.

Store date of birth as a String of “YYYY-MM-DD” format.

Graham Dickinson

unread,
Nov 9, 2020, 10:58:53 AM11/9/20
to Suzuki Tomohiro, sarfaraz ahmed, Flutter Development (flutter-dev)

Hi Suzuki,

You are of course correct, DoBs should be stored as strings. 

I was more focusing on the reading and writing of Firestore timestamps in Flutter. It does also seem that a firestore add of a Dart datetime converts it to a timestamp automatically anyway. 

sarfaraz ahmed

unread,
Nov 9, 2020, 4:49:27 PM11/9/20
to Flutter Development (flutter-dev)
Hey Guys,

You are too good. So, What I understood here is Any Date like Date of Birth, order Date, delivery date should be stored as String. Only when you need ServerTime Stamp... you should your timestamp type is firestore. But will that not make querying difficult as you would not able to run query with date in where clause.

I heard DateTime from Dart is automatically converted to Timestamp in firestore. But can you take look at toJSon function and tell me how to pass TimeStamp to firebase. 
----------------------------------------------

@JsonSerializable(nullable: false)
class UserProfileModel {
  String aboutMe;

  @JsonKey(fromJson: _fromJson, toJson: _toJson)
  DateTime dateofbirth;
  String business;
  String gender;
  String city;
  String pin;
  String state;

  UserProfileModel(
      {this.aboutMe,
      this.dateofbirth,
      this.business,
      this.gender,

      this.city,
      this.pin,
      this.state});

  factory UserProfileModel.fromJson(Map<String, dynamic> data) =>
      _$UserProfileModelFromJson(data);

  Map<String, dynamic> toJson() => _$UserProfileModelToJson(this);

  static DateTime _fromJson(String String) => DateTime.tryParse(String);
      
      
  static String _toJson(DateTime time) => time.toIso8601String();
}


Regards
Sarfaraz Ahmed

Suzuki Tomohiro

unread,
Nov 9, 2020, 5:00:25 PM11/9/20
to sarfaraz ahmed, Flutter Development (flutter-dev)
For timestamp if online orders I would use Timestamp, because my Amazon order placed at 17:00 Nov 9th in New York is actually on Nov 10th in Japan.  The birthday of the people born on November 9th in New York remain November 9th even when they relocate to Japan in future (and use your app)

Timestamp has fromDatetime method.

Reply all
Reply to author
Forward
0 new messages