Home > OS >  How to json data call by using DateTime.parse method and compare with today date?
How to json data call by using DateTime.parse method and compare with today date?

Time:04-10

Trying to call data(date) from model class and filter it based on today_date and show on a listview.

     DateTime now = new DateTime.now();
  DateTime dt1 = DateTime.parse (jsondata);
   if(dt1.compareTo(now) < 0){
     print("DT1 is before now");
    }

CodePudding user response:

Lets assume that jsondata is a string and parsing to date time so try do it like this

final dateTime = DateTime.now();
 DateTime dt1 = DateTime.parse("2022-04-09 12:45:00");
  
 if(dt1.isBefore(dateTime)){
   log("is Before");
 }else if(dt1.isAfter(dateTime)){
    log("is After");
 }

edit

sample as your data view above is like "03.02.2022"

use date format on this part

import 'package:intl/intl.dart';

void main() {
 final dateTime = "03.02.2022"; // sample this is the data you gonna pass in the function
 final dateTimeN = DateTime.now();
 final fin = DateTime.parse(functionDate(dateTime));
  
 if(fin.isBefore(dateTimeN)){
   print("is Before");
 }else if(fin.isAfter(dateTimeN) ){
   print("is After");
 } 
  
 
}

functionDate(String? dateTIme){
  final dateFormat = DateFormat("dd.MM.yyyy");
 final parse = dateFormat.parse(dateTIme!);
  
  return "$parse";
}
    
// result is Before
  • Related