skip to Main Content

In Flutter I convert dates to milliseconds like this:

static DateTime createDateTimeNow() {
    return DateTime(DateTime.now().year, DateTime.now().month,
        DateTime.now().day, 0, 0, 0, 0, 0);
  }

// This create a number value like 1660341600000
final Number millisecondsNow = Helpers.createDateTimeNow().toUtc().millisecondsSinceEpoch;

// I can also format is as String to be presented based on the language
final String dateNow = DateFormat.yMMMMd(language).format(millisecondsNow);

Now I have Firebase Cloud Functions that should read these dates and send notification when they expire, but could I convert this number to a normal date in Firestore?

From Flutter I do something like this:

final DateTime dateNow = DateTime.fromMillisecondsSinceEpoch(1660341600000);

2

Answers


  1. You’re working too hard. Firestore Timestamp to Dart DateTime:

    final dt = aTimestamp.toDate();
    

    and from DateTime to Timestamp:

    final ts = Timestamp.fromDate(aDt);
    

    as described in https://pub.dev/documentation/cloud_firestore_platform_interface/latest/cloud_firestore_platform_interface/Timestamp-class.html

    and there’s no need to think about "milliseconds".

    Login or Signup to reply.
  2. You can use fromMillis( ) method from firestore.timestamp which can create new timestamp from the given number of milliseconds.

    firestore.Timestamp.fromMillis(milliseconds)
    

    You can refer these documents for more details firestore timestamp from milliseconds and Firestore Timestamp

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search