Home > other >  send Timestamp to firebase
send Timestamp to firebase

Time:01-22

i try to make all date in the fire base at the same time zone for users from all the world how i got the same result from this two lines of code , thought i changed the timezone of the date and still the local date give the same timestamp as UTC time zone

        print(Timestamp.fromDate(DateTime.now()).toDate());
        print(Timestamp.fromDate(DateTime.now().toUtc()).toDate());
        print(Timestamp.fromDate(DateTime.now()).toDate());
        print(Timestamp.fromDate(DateTime.now().toUtc()).toDate());

CodePudding user response:

The DateTime type has a DateTime.utc() constructor you can use to create a UTC time:

print(DateTime.now());
    print(DateTime.now().toUtc());
    print(DateTime.utc(
      DateTime.now().year,
      DateTime.now().month,
      DateTime.now().day,
      DateTime.now().hour,
      DateTime.now().minute,
      DateTime.now().second,
    ).toUtc());

By default, the DateTime.now() constructor is in the user's local time:

print(DateTime.now().isUtc); // false
  • Related