i want to get time now in Cairo using timezone package independent of device date
but unfortunately it gives me the device date time . i don't know where is the error hope some one help me . the code below
DateTime returnCommonDate(){
tz.initializeTimeZones();
final egyptTimeZone = tz.getLocation('Africa/Cairo');
final now = tz.TZDateTime.now(egyptTimeZone);
print("=========================this is egypt time ${now}");
return now;
}
Related
Is there any way to check if the system DateTime is correct or not in Flutter? I tried searching for APIs which provide current DateTime, but the problem with timezones happens again. What is the solution for this? (I want to do something like WhatsApp. which is, don't give chatting access if system DateTime is wrong)
you can use ntp plugin
var nowInternet = await NTP.now();
var now = DateTime.now();
print(nowInternet);
print(now);
//check diff is grater than 1 minute
if (nowInternet.difference(now).inMinutes < 1){
//good
}
simple solution, use unix epoch time
print(DateTime.now().millisecondsSinceEpoch);
I have to display in a Flutter app, the time (HH:MM) of a flight in the local time of the country where the flight is taking off or landing. And NOT according to the timezone of the mobile device
So, how can I get the specific time format (HH:MM) of a Timezone, knowing that I got 2 different TZ format (according to source). Either such as Europe/London or could be +04:00
I have seen possibly 2 libraries timezone or time_machine but don't know if it is good fit
Many thanks
Unfortunately, the DateTime class of Dart only supports UTC and the local timezone. I suggest using the following third party package:
https://pub.dev/packages/timezone
It supports time zones:
import 'package:timezone/standalone.dart' as tz;
Future<void> setup() async {
await tz.initializeTimeZone();
var detroit = tz.getLocation('America/Detroit');
var now = tz.TZDateTime.now(detroit);
}
I currently have the following use case:
User receives a date in UTC from the backend
This date is transformed into local time for displaying purposes
The date is displayed in different inputs. One input for date and other for time
User can select time independently
The date should be sent back to the backend in UTC format as well
I'm not very experienced with time zones and I'm getting beaten by trying to allow the user to set up only the time (or date) in a datetime field.
My pseudo code is the following:
When receiving the from backend simply convert the date to show it to the user, making the orignal date stay in UTC
When the user picks the hour and minute (all in one action) use setHours and setMinutes from date-fns library
Use native toISOString() to set in models
Code so far [playground]:
import { utcToZonedTime, format } from "date-fns-tz";
import { setHours, setMinutes } from "date-fns";
const UTCStringdate = "2022-04-06T10:00:00.000000Z";
const userTimezone = "Asia/Bangkok";
const localizedTime = utcToZonedTime(UTCStringdate, userTimezone);
// Prints the correct information
// 10:00 in UTC is equal to 17:00 in Bangkok
console.log(format(localizedTime, "HH:mm"));
// Now I expext to set only the `minutes` and `hours`
// to this `localizedTime` so that I can update the backend
const [hours, minutes] = "10:30".split(":");
// Somewhere over here the `setHours` and `setMinutes`
// is turning the Date object into my own timezone
// and not using `Asia/Bangkok` timezone anymore
let newTime = setHours(localizedTime, hours);
newTime = setMinutes(newTime, minutes);
// Now I expect to print 17:30 since we only
// set up 30 minutes forward than the original one
// but it ends up printing 10:30
console.log(format(newTime, 'HH:mm'));
I understand that somewhere along the way (most likely in setHours and setMinutes) the date-fns library turns back the localizedTime back into my own timezone, completely ruining the idea of turning the Asia/Bangkok time into UTC.
Questions
First, is this the best approach to manipulate only the time part of a date when considering timezones? If not, anyone can point me to articles? I wasn't able to find anything on the topic
Second, how can I use setHours and setMinutes and still maintain the timezone?
There are no multiple time zones in JavaScript. There is UTC and there is your local one. What date-fns-tz does, is adjusting the time to the chosen user time zone. In your example, you can see this when printing both the original and the localized time
const utcTime = new Date(UTCStringdate);
console.log(utcTime.toISOString()); // -> 2022-04-06T10:00:00.000Z
console.log(localizedTime.toISOString()); // -> 2022-04-06T14:00:00.000Z
To solve your issue, convert UTC time to users time and let the user to adjust hours and minutes in his local time zone. After that, convert the time object back to UTC using zonedTimeToUtc
newTime = zonedTimeToUtc(newTime, userTimezone);
and then use newTime.toISOString() to send it back to the server.
console.log(newTime.toISOString());
Is there a way of getting the first day of the week from device info or locale in Flutter? I use some libraries for which it has to be manually set, so I need to get it somehow. Or maybe, at least, these is even a library you might know which returns first day of the week by locale code?
You can use subtract method to get first day of the week according to user's device locate
DateTime today = DateTime.now();
_firstDayOfTheweek = today.subtract(new Duration(days: today.weekday));
Or you can do that with material localization and get it from context
MaterialLocalizations.of(context).firstDayOfWeekIndex;
I would like to display the current time during my simulation so that I can simulate with full screen and also have the time. I tried the function getMinute(Date date) and a variable date. I didn't get an error but I also didn't get the time. Is it also possible to have a clock which displays the time?
You can do this:
String.format("%02d:%02d",getHourOfDay(), getMinute())
This will give you a string with the current time during the simulation
You can also use the clock object in the pictures palette to display time
you can use date() to know the date and time