Parsing date in dart/Flutter to calculate day difference - date

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

Related

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

Convert epoch time into timestamp flutter

I have a date-time in epoch format: 1633247247
I want to convert it into timestamps like this: Sunday, 3 October 2021 or just October 3 2021
I am writing this code
final DateTime timeStamp = DateTime.fromMillisecondsSinceEpoch(1633247247);
But it is returning 1970-01-19 18:25:11.247
Edit I ran this code
final DateTime timeStamp = DateTime.fromMillisecondsSinceEpoch(1633247247 * 1000);
Got the output in datetime. I am now trying to convert into string
String formattedDate = DateFormat('yyyy-MM-dd – kk:mm').format(date);
It gives this error The instance member 'date' can't be accessed in an initializer.
It represent to milliseconds, you need to multiple it with 1000 like below :
final DateTime timeStamp = DateTime.fromMillisecondsSinceEpoch(1633247247 * 1000);
It seems like that the time you've is 'seconds since epoch' so just multiplying by 1000 should give you the correct time.
final DateTime timeStamp = DateTime.fromMillisecondsSinceEpoch(1633247247 * 1000);
Download and Import
intl Pacakge
import 'package:intl/intl.dart';
Initialize a variable
String datetime;
Get Current date and conversion
Future<void> Datetimes() async{
setState(() {
DateTime now = DateTime.now();
String formattedDate = DateFormat('EEEE d MMM hh:mm:ss').format(now);
datetime=formattedDate;
});
}
Output
Saturday 3 oct 2:07:29
Refer this for DateandTime class for more formatting parameters
DateFormat-class
You need to use DateTime Formats. You can find a solution here:
https://stackoverflow.com/a/51579740/11604909

Days calculation difference in flutter json Api date - current date

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);

Passing date in Date data type only in groovy

I have been struggling with this even after doing so much of research on such a simple thing, so I need some help here.
I need to pass current date in date data type only in 'yyyy-MM-dd' format. SimpleDateFormat converts current date to string type and while trying to parse though it gets converted to Date type but changes the format.
I need currentDate in the format 'yyyy-MM-dd' of Date Type.
if(!session.dtfromDate && !session.dttoDate)
eq("startDate", currentDate)
I have managed to figure out a solutions to this. First got my desired format which obviously converted it to a String and then parsed this to a Date. It has worked perfectly fine.
SimpleDateFormat nsdf = new SimpleDateFormat('yyyy-MM-dd')
String currentDate = new SimpleDateFormat('yyyy-MM-dd').format(new Date());
Date newDate = (Date)nsdf.parse(currentDate)
if(!session.dtfromDate && !session.dttoDate)
eq("startDate", newDate)

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