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.
Related
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
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;
This question already has answers here:
How do I convert a date/time string to a DateTime object in Dart?
(8 answers)
unable to convert string date in Format yyyyMMddHHmmss to DateTime dart
(11 answers)
Closed last year.
Hi i have an array contains
List<dynamic> am = ['09:00', '09:30', '10:00', '10:30', '11:00', '11:30'];
and if they are selected
String time = '09:00'
how can I change time to the DateTime value or TimeOfDay
import 'package:intl/intl.dart';
if your time format is fixed (for example "hours:minutes" ) you can use this method
DateTime? _convertStringToDateTime(String time){
DateTime? _dateTime;
try{
_dateTime = DateFormat("hh:mm").parse(time);
}catch(e){}
return _dateTime;
}
You can use this:
TimeOfDay toTimeOfDay(String time){
List<String> timeSplit = time.split(":");
int hour = int.parse(timeSplit.first);
int minute = int.parse(timeSplit.last);
return TimeOfDay(hour: hour, minute: minute);
}
https://pub.dev/packages/date_format
See this package, By means of this package you can format your DateTime.
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);
}
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);
}