flutter datetime how to convert it - flutter

I have the following datetime from a database: 2022-10-02T23:10:24.736Z I want to compare it to a datetime now and get the difference in second. Basically I have the code for it, but im not able to convert the datetime.now to the mentioned format. It tells me it has a difference of 122 minutes but it should only be 2 minutes..
DateTime datenow = DateTime.now();
DateTime dateFormDatabase = DateTime.parse(widget.data[widget.currIndex-1]["roundEnd"]);
print(datenow); // <---- 2022-10-01 23:22:45.522687
print(dateFormDatabase); // <--- 2022-10-01 23:25:24.736Z
var diff = dateFormDatabase.difference(datenow);
print(diff.inMinutes); // <--- 122 but it should be 2minutes

The returned difference is correct. You are probably looking at different timezones here. DateTime.now() returns the current date and time in your local timezone. The parsed date from your database seems to be coming in as UTC. Notice the "Z" at the end here: 2022-10-01 23:25:24.736Z.
If you want to visually compare them you could convert either of those to UTC/local with toUtc or toLocal

Related

how to find difference btw 21:00 pm - 1:00 am in flutter

can someone please humbly help me? im fining it extremely confusing.
the dates are like this
1st date ~ 2 May 21:00
2nd date ~ 3 May 04:00
Let's say we have two dates.
var d1 = '2 May 04:00';
var d2 = '3 May 05:00';
First, add this intl package to your project and import it
import 'package:intl/intl.dart';
Next, you need to set a dateformat for your dates above:
var dFormat = DateFormat('d MMM HH:mm');
The date format should be same as your dates orelse you will get an error.
Next, convert the datetime string to your desired format.
var dateTime1 = dFormat.parse(d1, true);
var dateTime2 = dFormat.parse(d2, true);
You can set true if you want UTC time.
Lastly, to get difference between two dates just do the following:
print(dateTime2.difference(dateTime1).inHours); //25
The above print statement will return 25 as the difference between both the given time is 25hours. You can get difference in Minutes too.
Happy Coding!

Get every round hour from Firestore

I have a data stored in Firestore, the data add to Firestore every second, so in 24 hours I have 1440 documents (24 * 60), I want to fetch only round hour from the Firestore for show it on a Graph, how can I get only round hour from Firestore?
First of all, you will have to store the firestore documents with a timestamp property and then query the documents with the timestamp value which gives a rounded hour using DateTime and DateFormat APIs of flutter.
Once you've got a timestamp back from Firestore, something like :
Timestamp(seconds=1560523991, nanoseconds=286000000)
You can get only rounded hour from the timestamp value using 2 ways :
You need to parse it into an object of type DateTime:
DateTime myDateTime = (snapshot.data.documents[index].data['timestamp']).toDate(); // prints 2020-05-09 15:27:04.074
This will return your Firestore timestamp in the dart's DateTime format. In order to convert your DateTime object you can use DateFormat class from the intl package. You can use your obtained DateTime object to get the format of your choice like this:
Possible Solution 1 :
DateFormat.Hm().format(myDateTime); //prints 15.27
So the query should look for documents with:
DateFormat.m().format(myDateTime) to be “00” as
DateFormat.m() returns minutes and if minutes == 00 then the timestamp is rounded to the nearest hour.
Possible Solution 2 :
Convert timestamp into timestring using :
Let TimeString = snapshot.data.documents[index].data['timestamp']).toDate().toString() // prints ‘2019-12-28 18:48:48.364’
Let time = TimeString.split(“ “)[1] // prints 18:48:48.364
To check if the minutes is “00” :
if time.substring(3,5) == “00”, then it's a rounded hour. //here it is 48
You will have to put these timestamp conversion logics and then query as I mentioned for the rounded hour timestamp. There are no readymade available functions/methods to get the firestore documents with rounded hours.
Firestore supports "IN" Queries.
Store as Timestamp, and try with following
const roundhour1 = new Date('2021-10-01T01:00:00.000z');
const roundhour2 = new Date('2021-10-01T02:00:00.000z');
And then write the query like below:
database.collection("collectionName").where("fieldName", "in", ["roundhour1", "roundhour2"]);
You can have up to 10 values (roundhourX) to check "IN" of, so need to fire 3 queries to get all 24 hours data.

Converting timestamp object to date with difference in flutter

Let’s say I have an object of type Timestamp set to 2 days from now.
How to get the difference in time and then format it in DateFormat('hh : mm : ss') in dart ?
Construct a DateTime from the Timestamp.
Subtract from that DateTime the initial time (which I presume should be DateTime.now()) to get a Duration.
Format the Duration. Its default .toString() implementation uses hh:mm:ss.microseconds, which is close to what you want. If you want more control, you can easily format it yourself.
var dateTime = DateTime.fromMicrosecondsSinceEpoch(timestamp.timestamp!);
var duration = dateTime.difference(DateTime.now());
print(duration);

Display the difference between two Firestore Timestamp values in Flutter

I am taking startdate and endDate as input from user with DateTime as datatype in Flutter(Dart). These fields will be stored in Firestore may be as timestamp format.
Now we need to display the difference of endDate and startDate on client side which can be a live timer in format as "13 Hrs 45 mins" then after some mins it should be "13 Hrs 42 mins".
May I know how can we achieve this using Firestore or may be cloud functions for Flutter apps?
Thanks
In flutter, Timestamp can be easily converted to DateTime, and DateTime has convenient method called difference that will give you difference between two DateTime objects as Duration object.
Here is a simple example:
final Timestamp endTime = Timestamp(1000, 0);
final Timestamp startTime = Timestamp(500, 0);
final DateTime endTimeDate = endTime.toDate();
final DateTime startTimeDate = startTime.toDate();
final Duration difference = endTimeDate.difference(startTimeDate);

MomentJS date string adds one day

I don't understand why this date is saved as +1 day:
startdate = "2017-11-29T23:59:59.999Z";
var new_date = moment(startdate).format('DD/MM/YYYY'); // --> gives 30/11/2017
But if I do:
startdate = "2017-11-29";
var new_date = moment(startdate).format('DD/MM/YYYY'); // --> gives the correct date 29/11/2017
Any ideas?
Here is a jsfiddle showing this: http://jsfiddle.net/jbgUt/416/
Thanks!
If a time part is included, an offset from UTC can also be included as +-HH:mm, +-HHmm, +-HH or Z.
Add utc() to avoid it.
moment(startdate).utc().format('DD-MM-YYYY')
or
moment.utc(startdate).format('DD-MM-YYYY')
If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment()
Late to the party on this one, but I did just convert a few of our product's date-time objects to https://moment.github.io/luxon/
Takes out the need for the .utc() method above.