How to compare two unix time in flutter?

Issue

I want to check the start time and end time using unix time format in flutter.
I have a time x, I need to check the ‘x’ time is between ‘y’ and ‘z’ or equal to ‘y’ and ‘z’.

Note : The x,y,z times are in unix time format.

Solution

      static bool calculateTimeDiff(int yourtime, String givenY,
          String givenZ) {
                //your time, if your time in X in second 
        int x = yourtime;
             //if your your time is unix;
        String yDate = givenY.replaceAll('T', ' ');
        String zDate = givenZ.replaceAll('T', ' ');
    
    
        // parse date into standard datetime formate
        DateTime parseYDate =
        new DateFormat("yyyy-MM-dd HH:mm:ss").parse(yDate);
        DateTime parseZDate =
        new DateFormat("yyyy-MM-dd HH:mm:ss").parse(zDate);
    
        // convert date time into second
        int y = parseYDate.second;
        int z = parseZDate.second;

//if x,y,z all are integer value no need write above code. use just below code
        if (x >= y && x <= z) {
          return true;
        } else if (x == y||x==z) {
          return true;
        } 
    
        return false;
      }

Answered By – Babul

Answer Checked By – Senaida (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *