ebg11
ebg11

Reputation: 1046

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,

Upvotes: 0

Views: 1834

Answers (3)

Jama Mohamed
Jama Mohamed

Reputation: 3405

Try out this package, Jiffy

First, parse the string into a Jiffy instance

Jiffy jiffy1 = Jiffy.parse("2019-12-01", isUtc: true);
Jiffy jiffy2 = Jiffy.parse("2019-12-20", isUtc: true);

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

Upvotes: 0

Ulaş Kasım
Ulaş Kasım

Reputation: 870

just compare them with

DateTime date1 = DateTime.now();
DateTime date2 = DateTime.now();

if(date1.millisecondsSinceEpoch > date2.millisecondsSinceEpoch){
    return false;
}
return true;

Upvotes: 0

ahmetakil
ahmetakil

Reputation: 908

with intl package (https://pub.dev/packages/intl) you can convert those strings to actual date objects and then compare them easily

Upvotes: 1

Related Questions