Kitereative Indonesia
Kitereative Indonesia

Reputation: 1207

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

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.

Upvotes: 10

Views: 11545

Answers (2)

carloswm85
carloswm85

Reputation: 2396

This answer may not be useful for this specific scenario, but it is what worked for me when dealing with different time zone readings in the Windows app, despite having the correct data from the DB.

Let's say I had from the DB the time 8:30 AM and the Windows app was reading 11:30 AM, a 3 hours time difference.

I had to change the code from:

DateTime.parse(json["ExpeditionDateTime"])

to:

DateTime.parse(json["ExpeditionDateTime"]).toLocal()

Upvotes: 0

wangdq
wangdq

Reputation: 1994

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

Upvotes: 17

Related Questions