flutter - DateTime.now() not same in my local time

5,745

Well, this waste me lot of time, I answer it to help someone may encounter same problem. After modify emulator settings, it work OK. enter image description here

Share:
5,745
Muhammad Imanudin
Author by

Muhammad Imanudin

Updated on December 08, 2022

Comments

  • Muhammad Imanudin
    Muhammad Imanudin over 1 year

    I have a case to check the store opening-closed hours. Im solve this using the code below:

    final _openHours = 09;
    final _openMinute = 00;
    final _closeHours = 15;
    final _closeMinute = 00;
    
    var now = DateTime.now();
    print(now);
    
    var _open = new DateTime(now.year, now.month, now.day, _openHours, _openMinute, now.second);
    var _close = new DateTime(now.year, now.month, now.day, _closeHours, _closeMinute, now.second);
    
    now.isAfter(_open) && now.isBefore(_close) {
      print("online");  
    } else {
      print("offline");
    }
    

    but when I print DateTime.now(), this time does not match the current time? I have tried it using manually input currently time to make sure the code checks the opening and closing hours, and its works.