How to fix time in queryParameters in flutter? - flutter

I want to make a get request with DateTime inside query parameters, but DateTime takes not that form that I want. How to fix that?
I want to set this currentDate to query parameter
final currentDate = DateFormat('yyyy-MM-ddTHH:mm:ss').format(DateTime.now().toLocal());
log(currentDate); //from=2022-12-06T16:17:46
But I get this
final data = await _apiService.getNfbByPeriod(from: currentDate, to: DateTime(2022), token: accessToken!);
// currentDate = 2022-12-06T16%3A17%3A46
// /sessions/avg-nfb-by-period?from=2022-12-06T16%3A17%3A46&to=2022-01-01+00%3A00%3A00.000

Related

How to get difference between 1 Timestamp and 1 dateTime in minute or day

using DateTime we can use the following to get the difference
DateTime myFirstDateTime = DateTime.now();
DateTime mySecondtDateTime = DateTime.now();
myFirstDateTime.difference(mySecondtDateTime).inMinutes // or inDay
the previous code is working as expected but how can I handle the same between Timestamp and DateTime ?
I used the following but it give wrong result and strange negative numbers like -5623
Timestamp myTimestamp = // here I get the value from my Firebase field
DateTime myDateTime = DateTime.now();
then I convert it `toDate()`
myDateTime.difference(myTimestamp.toDate()).inMinutes; //or inDay
//output strange value
How can I do it? Thanks
You might have to move the brackets as follows:
myDateTime.difference(myTimestamp.toDate()).inMinutes;
Try this one out
var myTimestamp = // here you get the value from your Firebase field
DateTime myDateTime = DateTime.now();
DateTime myFirebaseTime = DateTime.parse(myTimestamp.toDate().toString());
myDateTime.difference(myFirebaseTime).inDays;

How can we identify time section in datetime flutter

I will receive DateTime in UTC format from API and I need to convert the DateTime to local time zone based on the condition.
We have toLocal() method to change the time based on the device time zone.
condition: 23-4-2021 // no need to change it to toLocal()
23-4-2021 00:00:00 // no need to change it to toLocal()
23-4-2021 10:30:34 // need to change it to toLocal()
If we have time in the DateTime then only we have to change it in local time.
DateTime utcToDateTimeLocal(DateTime value) {
return value.toLocal();
}
Thanks!
Something like this maybe ?
DateTime utcToDateTimeLocal(DateTime value) {
if (value.hour!=0 || value.minute!=0 || value.second!=0){
return value.toLocal();
}
return value;
}
Here is quick solution that work for me.
You can get time (or any format) by DateFormat class
In your case
dateTime = '23-4-2021 10:30:34'
final format = DateFormat('HH:mm a');
final clockString = format.format(dateTime);
you will get // 10:30 AM
DateTime utcToDateTimeLocal(DateTime value) {
var dateFormat =
DateFormat("dd-mm-yyyy hh:mm a"); // you can change the format here
var utcDate =
dateFormat.format(DateTime.parse(value.toString())); // pass the UTC time here
var localDate = dateFormat.parse(utcDate, true).toLocal().toString();
return DateTime.parse(localDate);
}

How to convert date and time only at time?

I get by API such data about time and date 2021-09-05T18:16:47.790601+02:00 how can I get only hours from here?
From the API you're getting a formatted date and want to get only the hour.
First, it would be better to you get from the API the UNIX date (if available) which has a timestamp format like this: https://www.unixtimestamp.com
Your first question would be more either:
How getting from a fromatted date a Timestamp to obtain the hour
or
How parsing the string to get the hour.
Here the formatted method:
DateTime which should help you: https://api.flutter.dev/flutter/dart-core/DateTime-class.html
Here an example:
final DateTime myDate = DateTime.parse('2021-09-05 18:16:47');
final int hour = myDate.hour;
final int minute = myDate.minute;
final int second = myDate.second;
print('hour: $hour');
print('minute: $minute');
print('second: $second');
Result:
hour: 18
minute: 16
second: 47
Here the parsing method:
Substring which should help you: enter link description here
RegExp which should help you: enter link description here
Here an example:
const String myDate = '2021-09-05T18:16:47.790601+02:00';
final String hour = myDate.substring(11, 13);
final String minute = myDate.substring(14, 16);
final String second = myDate.substring(17, 19);
final String date = RegExp(r"[0-9]{2}:[0-9]{2}:[0-9]{2}").stringMatch(myDate).toString();
final List<String> splitDate = date.split(':');
print('hour: $hour');
print('minute: $minute');
print('second: $second');
print('date: $date');
print('split date: $splitDate');
Result:
hour: 18
minute: 16
second: 47
date: 18:16:47
split date: [18, 16, 47]
For ISO8601 String you can convert with DateTime
DateTime dt = DateTime.parse('2020-01-02 03:04:05');
then you can access what you want with dt

Want to compare two dates and take out the difference in days

I am getting Timestamp from Firebase Firestore and then formatting it into date and then want to take out the difference in days. And for that I am doing this:-
DateTime myDateTime = DateTime.parse(nameTimestamp!=null? nameTimestamp.toDate().toString() :DateTime.now().toString());
print('$myDateTime');
String formattedDateTime = DateFormat('yyyy,MM,dd').format(myDateTime);
print('$formattedDateTime');
final nameChangeDate = DateTime(2021,6,1);
final todayDate = DateTime.now();
final difference = todayDate.difference(nameChangeDate).inDays;
print(difference);
And now the problem I am facing is if I put formattedDateTime in front of nameChangeDate like mentioned below then at difference there's appear an error that The argument type 'String' can't be assigned to the parameter type 'DateTime'.
final nameChangeDate = formattedDateTime;
final todayDate = DateTime.now();
final difference = todayDate.difference(nameChangeDate).inDays; ///Error
print(difference);
Help me in this or guide me to take out the difference in some other way.

How to store dates in a List<DateTime> in flutter?

I am trying to use a PageView.builder in my application and i wanted each page to display a particular date. I have defined a list of type DateTime : List<DateTime> _month;. how do i store all the days in a particular month (30 days) in the list _month?
The stored dates must be of type DateTime in order to implement this in my application.
This code will do what you need. I don't know the entire structure of your code so I just wrote this example!
void main() {
int month = 1;
DateTime start = DateTime(2019,month);
DateTime end = DateTime(2019,month+1);
int c = (end.toUtc().difference(start.toUtc()).inDays);
List<DateTime> _month = [];
_month.addAll(List.generate(c,(index) => start.toUtc().add(Duration(days:index)).toLocal()));
print(_month);
}