Ad
Why Can't I Add Offset Hours In Datetime Flutter Project?
I tried to add offset hours to my datetime object now, but it doesn't show the added time that is actual time. It is my flutter project in android studio.
void getTime() async {
Response response = await
get('http://worldtimeapi.org/api/timezone/Asia/Karachi');
Map data = jsonDecode(response.body);
String datetime = data['datetime'];
String offset = data['utc_offset'].substring(1,3);
print(offset);
DateTime now = DateTime.parse(datetime);
now.add(Duration(hours: int.parse('offset')));
print(now);
}
@override
void initState() {
super.initState();
getTime();
}
This is the result I got: Screenshot of the output
Ad
Answer
I found the solution. Yayyy!
void getTime() async {
//Make the request
Response response = await get('http://worldtimeapi.org/api/timezone/Asia/Karachi');
Map data = jsonDecode(response.body);
print(data);
//Get properties from data
String datetime = data['datetime'];
String offset = data['utc_offset'].substring(1,3);
//print(datetime);
print(offset);
//create DateTime Object
DateTime now = DateTime.parse(datetime);
now = now.add(Duration(hours: int.parse(offset)));
print(now);
}
@override
void initState() {
super.initState();
getTime();
}
Ad
source: stackoverflow.com
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View
Ad