Days calculation difference in flutter json Api date - current date - flutter

I am fetching one date-format from json Api and another date is current date i wants the difference in days like this eg;-
server_date=2020-04-18T17:26:33.150;
current_date=2020-05-07;
var days=server_date - current date;
Json Api "CLMM_LAST_ACTIVITY_DT": "2020-04-18T17:26:33.150",

you can use difference method of dataTime.
following code help you more.
String server_date = "2020-04-18T17:26:33.150";
DateTime currentTime = DateTime.now();
int days = currentTime.difference(DateTime.parse(server_date)).inDays;
print(days);

Related

Date string not recognized as valid DateTime

I am storing a date string in SharedPreferences as 2022-9-14 19:34, but this is not recognized as DateTime when using
DateTime dt1 = DateTime.parse(_inicio);
print("fecha······· ${(dt1)}");
I am getting the following error:
Invalid date format
2022-9-14 19:34
What I need is to show the date string as 14-09-2022 19:34
EDIT
The date string is saved as follows:
var hoy = DateTime.now();
var ano = hoy.year.toString();
var mes = hoy.month.toString();
var dia = hoy.day.toString();
var turno = "";
var fechaHora = ano+"-"+mes+"-"+dia+" "+_timeOfDay.format(context).toString();
And _timeOfDay is the picked time from TimePicker.
2022-09-14 19:34 is one of the valid case. So you need to add zeros where there is a single digit (month in this case)
But a better solution would be to store DateTime.now().toString() directly in shared-preferences. You could also use a package like intl to format the date in your choice of format.
Since you're using time as well from timepicker, you could use DateTime constructor will all 5 values like this:
new DateTime(year, month, date, hour, minute).toString();

Flutter how to convert timestamp date time

I am getting date and time from store. In data base its look like this
Need to know how can I show this as DD/MM/YY
I am trying to do like this
String timeString = snapshot.data[index]['lastupdate'].toString();
DateTime date = DateTime.parse(timeString);
print(DateFormat('yyyy-MM-dd').format(date));
Its showing error of Invalid date format
Try like this your lastupdate date is not convertime inDate thats why its showing error
DateTime date = DateTime.parse(snapshot.data[index]['lastupdate'].toDate().toString());
print(DateFormat('dd-MMM-yyy').format(date));
Use function to get date from Timestamp like this:
readDate(Timestamp dateTime) {
DateTime date = DateTime.parse(dateTime.toDate().toString());
// add DateFormat What you want. Look at the below comment example
//String formatedDate = DateFormat('dd-MMM-yyy').format(date);
String formatedDate = DateFormat.yMMMMd().format(date);
return formatedDate;
}
Use the function when you want it.
For example:
Text(readDOB(streamSnapshot.data!["dob"]))
For this you should install intl package. read

How to parse a Datetime with format "MM/dd/YY" in flutter

So far I try new DateFormat("MM/dd/yy").parse('04/03/20') but it produce 04/03/0020 date.
You have to formate this date using formate method of datetime class. However, you need to convert string to datetime using parse method because datetime formate take datatime as an argument.
Following line help you to achieve desire output:
print(DateFormat("MM/dd/yy")
.format(DateFormat("MM/dd/yy").parse('04/03/20')));
UPDATE:
This following code will give result as you expected but it is hard to find out it is 1920 or 2020?
DateTime _dateTime = DateFormat("mm/dd/yy").parse('04/03/20');
_dateTime = DateTime(2000 + _dateTime.year, _dateTime.month, _dateTime.day);
print(_dateTime);

Parsing date in dart/Flutter to calculate day difference

I am struggling to find a way to parse 2 YYYY-MM-DD dates in dart/Flutter. I need to find out whether a given date is before another date in terms of number of days in utc. If they are the same or it is in the future it should return false.
Thanks,
with intl package (https://pub.dev/packages/intl) you can convert those strings to actual date objects and then compare them easily
just compare them with
DateTime date1 = DateTime.now();
DateTime date2 = DateTime.now();
if(date1.millisecondsSinceEpoch > date2.millisecondsSinceEpoch){
return false;
}
return true;
Try out this package, Jiffy
First, parse the string into a Jiffy instance
Jiffy jiffy1 = Jiffy("2019-12-01")..utc();
Jiffy jiffy2 = Jiffy("2019-12-20")..utc();
Next is to check if it is the same or in the future in terms of days
jiffy1.isSameOrBefore(jiffy2, Units.DAY); // true
You can also check if it is the same or in the past in terms of days
jiffy1.isSameOrAfter(jiffy2, Units.DAY); // false
Check out this documentation for more
Hope this helped

How do I convert a Date to an int?

I have this
DateFormat dateFormat = new SimpleDateFormat("MM");
Date date = new Date();
I know the value as 10 but have got no idea how to print it out. How do I convert it to an int? If there even is a way?
date.getMonth()
it is deprecated - you should be using Calendar, but if you don't want to change, that'll do it. It is 0 indexed, so remember to change the resulting value appropriately.
Javadoc