Home > Enterprise >  I get a expiry date and time from the api how compare it with the current time?
I get a expiry date and time from the api how compare it with the current time?

Time:12-30

I'm getting a expiry time when i call the log in api in for format of "Thu, 30 Dec 2021 10:24:34 GMT" i want to get the current time and compare it.

CodePudding user response:

By using enter image description here

import 'package:intl/intl.dart';

void main() {
  String strDate = "Thu, 30 Dec 2021 10:24:34 GMT";
  DateFormat format =  DateFormat("EEE, dd MMM yyyy hh:mm:ss");
  
  
  DateTime parsedDate = format.parse(strDate);
  print('parsedDate: $parsedDate');
  
  DateTime now = DateTime.now();
  print('now: $now');
  
  print('parsedDate is before now: ${parsedDate.isBefore(now)}');
  print('parsedDate is after now: ${parsedDate.isAfter(now)}');
}

CodePudding user response:

If I understand your question correctly, this can help.

DateTime now = DateTime.now();
DateTime then = DateTime.parse(expirlyDateFromApi.toDate().toString());
var difference = now.difference(expirlyDate);

CodePudding user response:

Please follow this code. First you format your time then check the difference

bool _getEndDate(String endDate) {
  DateFormat format =  DateFormat("EEE, dd MMM yyyy hh:mm:ss");
  final date2 = DateTime.now();

  final difference = date2.difference(format.parse(endDate)).inSeconds;

  return difference <= 0 ? true : false;
}
  • Related