I have chat app. I want allow user message other user only every other week. For example:
Week 1 (allowed
Week 2 (not allowed)
Week 3 (allowed)
Week 4 (not allowed)
...
How I can do this? I want write conditional so can say:
if (allowedWeek) {
sendMessage
}
Maybe can use something like this?
DateTime.now().isAfter(X) && DateTime.now().isBefore(Y)
How I can tell DateTime this?
May be you can get week of the year by this package, week_of_year, and decide which week to talk which is not:
import 'package:week_of_year/week_of_year.dart';
void main() {
final date = DateTime.now();
print(date.weekOfYear); // Get the iso week of year
print(date.ordinalDate); // Get the ordinal date
print(date.isLeapYear); // Is this a leap year?
}
Related
I am newbie in learning coding,
hi i want to show show the post created time like 2hours ago or 30 minutes ago like that, how to do that i only have the created time like 2023-01-13 10:35:52", how to convert that with current time or day
you can use the difference method on date and time which give you the difference
final date2 = DateTime.now();
final difference = date2.difference(date);
print(difference.inHours);
from that you can get the difference in hours difference in minutes etc
check the timeago package, it helps implementing what you need
import 'package:timeago/timeago.dart' as timeago;
main() {
final date = DateTime.now().subtract(Duration(minutes: 50));
print(timeago.format(date)); // 50 minutes ago
}
When working on a Flutter/Dart project where I had to parse different methods entering dates into a form, I found that if the user wrote i.e (by accident) '09.13' for december 9th (wrote 13 instead of 12), the parse result for parse rolled over to january next year.
Given in code:
DateFormat format = 'dd.MM';
try{
var dateTime = format.parse('09.13');
print("dateTime = ${dateTime.toString()}");
}on FormatException{
print("dateTime error");
}
I expected this to be
dateTime error
but instead I got
dateTime = 1971-01-09 00:00:00.000
I understand the reset to 1970, since I didn't give any year, I'd rather expect it to give a FormatException to show the user that there is a flaw in the entered date... ...but rolling over to the next year (1971) and give no Exception ?
Is there any reason for this behavior, or do I simply need to fight with RegEx for this dateTime check...
this is actually, not something to fix, the DateFormat algorithm was made to calculate the expected DateTime from the input:
9 days and 13 months logically equal 1 year, 1 month, and 9 days.
However, using dart you can throw your own custom FormatException, as example you could do something like this:
try{
String stringDate = "09.13";
if(checkMonthLimit(stringDate)) {
var dateTime = format.parse(stringDate);
print("dateTime = ${dateTime.toString()}");
} else {
throw FormatException("some text here");
}
}on FormatException{
print("dateTime error");
}
bool checkMonthLimit(String txt) {
return int.parse(txt.split(".")[1]) <= 12;
}
Now if the month is above 12, the method will return false, so it enter the else block, then throws the FormatException, which will be catched in the catch block.
I want to check there is any time within certain time. Clearly, I got a list:
List<String> timeSlots = [];
This list returning me times like this: [08:30,09:00,...,22:00]
And I got current phone time. And I am creating listViewBuilder.
I just want to check if current phone time between 08:30-09:00 and I want to set my Card color which it's listViewBuilder's returning widget.
What I would do is use the isAfter & isBefore methods of DateTime. So you will have to create DateTimes out of the first two times :
List<String> timeSlots = ["08:30", "09:00", "22:00"];
DateTime currentTime = DateTime.now();
DateTime date1 = DateTime.parse(
"${currentTime.year}-${currentTime.month < 10 ? '0${currentTime.month}' : currentTime.month}-${currentTime.day} ${timeSlots[0]}:00");
DateTime date2 = DateTime.parse(
"${currentTime.year}-${currentTime.month < 10 ? '0${currentTime.month}' : currentTime.month}-${currentTime.day} ${timeSlots[1]}:00");
print(currentTime.isAfter(date1) && currentTime.isBefore(date2));
There may be other prettier ways though ^^
I am struggling to find a way to parse 2 YYYY-MM-DD dates in dart/Flutter. I need to find out whether a given date is before another date in terms of number of days in utc. If they are the same or it is in the future it should return false.
Thanks,
with intl package (https://pub.dev/packages/intl) you can convert those strings to actual date objects and then compare them easily
just compare them with
DateTime date1 = DateTime.now();
DateTime date2 = DateTime.now();
if(date1.millisecondsSinceEpoch > date2.millisecondsSinceEpoch){
return false;
}
return true;
Try out this package, Jiffy
First, parse the string into a Jiffy instance
Jiffy jiffy1 = Jiffy("2019-12-01")..utc();
Jiffy jiffy2 = Jiffy("2019-12-20")..utc();
Next is to check if it is the same or in the future in terms of days
jiffy1.isSameOrBefore(jiffy2, Units.DAY); // true
You can also check if it is the same or in the past in terms of days
jiffy1.isSameOrAfter(jiffy2, Units.DAY); // false
Check out this documentation for more
Hope this helped
So my question is pretty simple. I'd like to have specific dates that are enabled on a datepicker, like only the last day of any month, for example 30/06/2019, 31/07/2019
Primng Datepicker
<p-calendar formControlName="date"></p-calendar>
It is a simple hack for your requirement.
In your component.html use min and max date and (onMonthChange) method to update min and max value of date when user changes the month. Trick is just make min and max date equal to last day of the month.
<p-calendar [(ngModel)]="value" tabindex="0" [maxDate]="maxDateValue"
[minDate]="minDateValue" readonlyInput="true"
(onMonthChange)="onMonthChange($event)">
</p-calendar>
And in component.ts file use below code:
public maxDateValue: Date;
public minDateValue: Date;
ngOnInit() {
this.setMinMaxDate();
}
setMinMaxDate() {
var nowdate = new Date();
this.maxDateValue = new Date(nowdate.getFullYear(), nowdate.getMonth() + 1, 0);
this.minDateValue = new Date(nowdate.getFullYear(), nowdate.getMonth() + 1, 0);
}
// method to handle month change.
onMonthChange(e:any) {
this.minDateValue = new Date(e.year, e.month, 0);
this.maxDateValue = new Date(e.year, e.month, 0);
}
You must be thinking why at time of initialization I am using nowdate.getMonth() + 1, but inside onMonthChange only e.month, not adding + 1.
You need to go through this find last and first date of month article how to get the last and first date of a month and print the values of month in console and see the differences. Then you will understand easily.
If you just need one day of each month you should use the month view
<p-calendar [(ngModel)]="dateValue"
view="month" dateFormat="mm/yy"
[yearNavigator]="true"
yearRange="2000:2030"></p-calendar>