i have DateTime.now().toIso8601String() = 2022-08-09T03:01:32.223255
how can i find if 3 days have passed since the date ?
You can parse string to DateTime object:
final dateTime = DateTime.now();
final stringDateTime = dateTime.toIso8601String();
final parsedDateTime = DateTime.parse(stringDateTime);
In this case, dateTime and parseDateTime are the same.
Then to find out the time difference use difference DateTime instance method, which returns Duration instance. Example from dart documentation:
final berlinWallFell = DateTime.utc(1989, DateTime.november, 9);
final dDay = DateTime.utc(1944, DateTime.june, 6);
final difference = berlinWallFell.difference(dDay);
print(difference.inDays); // 16592
Use the difference function on DateTime object:
DateTime now = DateTime.now();
DateTime addedThreeDays = DateTime.now().add(Duration(days: 3));
print(3 >= now.difference(addedThreeDays).abs().inDays);
Related
In my app I'm converting a double temp by temp.toInt() to a late int temperature variable. But somehow my app crashes and showing me error saying "type 'int' is not a subtype of 'double'". The main problem is it works suddenly. And then again it crashes. I don't know why it's happening. here is my code-
class _LocationScreenState extends State<LocationScreen> {
WeatherModel weather = WeatherModel();
late int temperature;
late String cityName;
late String weatherIcon;
late String weatherMessage;
#override
void initState() {
super.initState();
updateUI(widget.locationWeather);
}
void updateUI(dynamic weatherData) {
setState(() {
if (weatherData == null) {
temperature = 0;
weatherIcon = 'Error';
weatherMessage = 'Unable to get weather data';
cityName = '';
return;
}
double temp = weatherData['main']['temp'];
temperature = temp.toInt();
var condition = weatherData['weather'][0]['id'];
weatherIcon = weather.getWeatherIcon(condition);
weatherMessage = weather.getMessage(temperature);
cityName = weatherData['name'];
});
}
what should I do? please let me know if you have any advice.
Thanks in advance.
I've tried declaring another int variable and assign it to temperature but that didn't work either.
Seeing the code and the error it seems the error must actually be on this line:
double temp = weatherData['main']['temp'];
Meaning that it already is an int and you can't assign that to the double here
you can probably just directly do
temperature = weatherData['main']['temp'];
Can you try making temp as dynamic
dynamic temperature;
The value is sometimes an int, sometimes a double. By converting weatherData['main']['temp'] to a double value -
weatherData['main']['temp'].toDouble();
solves the problem.
Dont use .toInt()
Use
temperature = int.parse(temp);
How to create hive object that includes an id field with auto increment integer value?
This is my hive object class and id always is -1. How should I fix it?
import 'package:hive_flutter/adapters.dart';
part 'reminder.g.dart';
#HiveType(typeId : 2 )
class Reminder extends HiveObject {
int id = -1;
#HiveField(0)
String title = '';
#HiveField(1)
String body = '';
#HiveField(2)
String dateFancyString = '' ;
#HiveField(3)
String timeFancyString = '' ;
#HiveField(4)
DateTime dateTime = DateTime.now();
}
I have a class that I use to get the number of days between 2 Instants :
public static Long getDaysBetween(final Instant startInstant, final Instant endInstant) {
final ZonedDateTime startDate = startInstant.atZone(ZoneId.systemDefault());
final ZonedDateTime endDate = endInstant.atZone(ZoneId.systemDefault());
return ChronoUnit.DAYS.between(startDate, endDate);
}
And the corresponding test class
class WeekCalculatorTest {
#Test
void givenStartAndEndInstant_whenCalculatingTheNumberOfDaysBetween_thenTheResultShouldBe() {
final String start = "2022-09-01T00:00:00Z";
final String end = "2022-10-31T00:00:00Z";
final Instant startInstant = Instant.parse(start);
final Instant endInstant = Instant.parse(end);
assertThat(WeekCalculator.getDaysBetween(startInstant, endInstant), is(59L));
}
}
I don't understand why when I change the end variable to final String end = "2022-10-30T00:00:00Z";, my test is still passing.
I have a current datetime with followint function :
String cdate = DateFormat("yyyy-MM-dd HH:mm:ss").format(DateTime.now());
Now I want to subtract 1 day or 24 hours from this current date time.
How can i do that?
Don't manipulate the string, manipulate the date. From docs, adapted:
final now = DateTime.now();
final yesterday = now.subtract(const Duration(days: 1));
Apply the formatting just before you need to display the date, not before.
Date manipulation methods are provided by the DateTime class. Refer to DateTime documentation. Checkout now, add, subtract.
The following code shows how you can achieve what you want:
DateTime today = DateTime.now();
String cdate = DateFormat("yyyy-MM-dd HH:mm:ss").format(today);
DateTime yesterday = today.subtract(Duration(days: 1));
String ydate = DateFormat("yyyy-MM-dd HH:mm:ss").format(yesterday);
Try below code also refer add and subtract for DateTime
void main() {
var date = new DateTime.now();
date = DateTime(date.year, date.month, date.day - 1);
// or use subtract method also like below
//final day= date.subtract(Duration(days: 1));
print(date);
print(day);
}
Result using day-1:
2022-09-13 00:00:00.000
Result using subtract method :
2022-09-13 12:52:45.734
final currentDay = DateTime.now();
final yesterday = now.subtract(const Duration(hours: 24));
I know such questions are repeated but I did not get any result.I must change helical date to Gregorian date and this is my method
public static DateTime ShamsiToMiladi(string shmasi)
{
DateTime? Dt = null;
if (shmasi != "")
{
System.Globalization.PersianCalendar persian = new System.Globalization.PersianCalendar();
char[] spChar = { '/' };
string[] splited_shamsi = shmasi.Split(spChar, 3);
Dt = persian.ToDateTime(int.Parse(splited_shamsi[0]), int.Parse(splited_shamsi[1]), int.Parse(splited_shamsi[2]), 12, 12, 12, 12);
}
return Dt;
}
sahmsi is a parameter that comes from a textbox.what do i return Dt?
thanks for help
Since DateTime is actually a structure built upon a long integer, it cannot be assigned a null value.
But if you need to have that option, declare it as a nullable DateTime thusly:
DateTime? ThisDate = null;
If you do return a nullable DateTime, your caller will have to check for a null value before using as a regular DateTime in expressions.
As a general rule, a newly-declared regular DateTime has a value of DateTime.MinValue ('01/01/0000'), and you can test for that, instead.