I see a strange behavior with the Jiffy package (link). If I run the following code I get the following output (the first is correct, the second is wrong)
42
43
final refDate = new DateTime(2020, 10, 18); // 18 Oct 2020
int weekNumber(DateTime date) {
int dayOfYear = int.parse(DateFormat("D").format(date));
return ((dayOfYear - date.weekday + 10) / 7).floor();
}
print(weekNumber(refDate));
print(Jiffy(refDate).week.toString());
Can anyone explain when? I see the .week function call the same formula... (link)
Thanks!
This seems to be the intended behaviour of the package, ie weeks to start on Sunday and end on Saturday. Hopefully the developer will give an option to set when the week should start
Related
hello guyz am new to flutter and am practicing on dates then i encounter a difficulty on how can i acheive the result same below using a loop or something in order to get this result.
so far i tried is
final now = DateTime.now();
for(var d = 1 ; d <= 5 ; d++){
// Don't know whats next to do here to get the same result below
}
i want result like this
Apr 19, 2022
Apr 26, 2022
May 3, 2022
May 10, 2022
May 17, 2022
can some help me and explain.
Dart's DateTime class has an add function, where you can specify a duration which will be added the current Date.
Therefore, what you can do is:
DateTime now = DateTime.now();
for(var d = 1; d <= 5; d++) {
...
now = now.add(Duration(days: 7)); // here you can specify your interval. Also possible: hours, minutes, seconds, milliseconds and microseconds
}
I'll recommend reading the Docs, e.g. DateTime Docs and Duration Docs
I am attempting to generate a PageView that will display the Month and Year related to the number of times you swipe in either directon.
Example 1:
I swipe right twice, so I get Feb 2021
Example 2:
I swipe left 12 times, so I get April 2020
I have attempted to create a DateTime.now() and subtract an integer of months, but I'm not having much luck. I have looked at various plugins like DateUtils, but again no luck.
I have been at what should be a simple solution for while now and would appreciate a guidance.
The closet I get is the following which requires me to know the days in each month which isn't ideal
(DateTime.now().subtract(Duration(days: 90)).toString())
From DataTime docunamtion:
Returns a new [DateTime] instance with [duration] added to [this].
var today = DateTime.now();
var fiftyDaysFromNow = today.add(const Duration(days: 50));
// adds 1 days
DateTime _future = DateTime.now().add(const Duration(days: 1));
//substracts 1 day
DateTime _tomorrow2 = DateTime.now().subtract(const Duration(days: 1));
Also this, credit ,define the base time, let us say:
var date = new DateTime(2018, 1, 13);
Now, you want the new date:
var newDate = new DateTime(date.year, date.month - 1, date.day);
And you will get
2017-12-13
Y'all are going about it wrong. Presuming 24 hours in a day, or 30 days in a month, is just wrong. Here's how to always get midnight the first of the month, 7 months before today:
void main() {
var n = DateTime.now();
print(DateTime(n.year, n.month - 7, 1));
}
Just use DateTime constructors. They wrap around just fine. Works at month's end as well:
void main() {
var n = DateTime(2021, 2, 28);
print(DateTime(n.year, n.month, n.day + 1));
n = DateTime(2020, 2, 28);
print(DateTime(n.year, n.month, n.day + 1));
}
Which correctly shows 3/1 for 2021, and 2/29 for 2020, as it was a leap year.
Stop adding 24-hour days! I've got a video that explains why.... https://www.youtube.com/watch?v=usFSVUEadyo
And here's a video that goes into this with more detail: Proper Month and Day Arithmetic in Dart and Flutter: youtu.be/LpoBYgzKVwU
I'm using Swift 5. I'm trying to get the number of weeks in a month. Jan 2021 prints 6 weeks, but May prints 5 weeks. I'm thinking the problem is firstWeekday is set to 1. Is there a way to get the number of weeks in a given month without setting firstWeekday?
var localCalendar = Calendar.current
localCalendar.firstWeekday = 1
let weekRange = localCalendar.range(of: .weekOfMonth, in: .month, for: dateFormatter.date(from: monthName)!)
if let weekRange = weekRange {
print("\(monthName) has \(weekRange.count) weeks.")
}
It seems you do not want the number of weeks in a month at all. You want the number of rows in a calendar printout.
The formula for the number of rows needed to represent a month on a standard Gregorian-style calendar is as follows.
Start with the number of days in the month. Add to that the number of blank days before the start of the month, beginning with Sunday. For example, January 2021 is 31 + 5 = 36, because it's 31 days long but 5 days (Sunday thru Thursday) are not part of it before the start. To put it another way: the first day of January 2021 is a Friday; that is day 5 of the week if we call Sunday day 0, so we get 31 + 5.
Now integer-divide by 7. We need at least that number of rows. So (31+5)/7 using integer division is 5. The question is: is that all the rows we need?
To find out, get the remainder of that division. If it is not zero, add another row. So (31+5)%7 is 1, which tells us that one more day needs to be accommodated so we need another row. That makes 6.
Thus:
// `startingOn` pretends that Sunday is day 0
func rowsNeededForMonthWith(numberOfDays n: Int, startingOn i: Int) -> Int {
let (quot,rem) = (n+i).quotientAndRemainder(dividingBy: 7)
return quot + (rem == 0 ? 0 : 1)
}
Here are some quick sanity tests:
// January 2021
rowsNeededForMonthWith(numberOfDays: 31, startingOn: 5) // 6
// But suppose it had 30 days?
rowsNeededForMonthWith(numberOfDays: 30, startingOn: 5) // 5
// Or suppose it had started on Thursday? (cf July 2021)
rowsNeededForMonthWith(numberOfDays: 31, startingOn: 4) // 5
I'd like to create a timer for a flutter website that counts down to a certain date.
e.g.
120 Days 12 Hours 45 Mins 15 Secs
Is that possible and can anyone help?
I haven't got a clue where to start with this one..
You can use this package
https://pub.dev/packages/countdown_flutter
Note: In this package you will see a duration property. You should locate time difference between nowDate and the other date
var nextDate = DateTime(2023, 10, 29);
var nowDate = DateTime.now();
var difference = nowDate.difference(nextDate);
What I am trying to do here is this - I want to give index to only the workdays in each week.
So, if in a week, Monday and Wednesday are holidays, then Tuesday should get 1, Thursday should get 2, Friday should get the index 3. Otherwise, in a normal week without any holidays, Monday should get 1, Tuesday 2, Wednesday 3, and so on ...
Here is the code I have written (I haven't coded in years now, so please pardon the crude approach)
Sheet 'Holidays' contains a list of holidays in the column B starting from row 2
Variable date is the date for which I want to find out the index for
Variable dayOfTheWeek is the number of day of 'date' counted from last Sunday, so if date is a Monday, dayOfTheWeek is 1; if date is Tuesday, dayOfTheWeek is 2, and so on ...
function indexOfWorkdayOfTheWeek (date, dayOfTheWeek, lastSundayDate)
{
var activeSheet = SpreadsheetApp.getActiveSpreadsheet();
var activeCell = activeSheet.getActiveRange();
var activeRow = activeCell.getRowIndex();
var activeColumn = activeCell.getColumn();
var count = 1;
for (var j = 1; j < dayOfTheWeek; j++)
{
var date2 = lastSundayDate.valueOf() + j*86400;
Logger.log('Date ' + j + ' is:' + date2);
Logger.log('Last Sunday is:' + lastSundayDate);
if (holidayOrNot(date2) == true)
{
}
else
{
count = count + 1;
}
}
return count;
}
function holidayOrNot(date2)
{
var holidaysSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Holidays');
var listOfHolidays = holidaysSheet.getSheetValues(2, 2, 95, 1);
var isDateMatch = false;
for (var k = 0; k < 90; k++)
{
if (date2 == listOfHolidays[k].valueOf())
{
isDateMatch = true;
break;
}
else
{
continue;
}
}
return isDateMatch;
}
I think the problem is two-fold here:
The date2 calculation isn't working for some reason (var date2 = lastSundayDate.valueOf() + j*86400;)
The function holidayOrNot is returning false, no matter what, even if it encounters a holiday ... the condition date2 == listOfHolidays[k] isn't working for some reason...
Help would be appreciated!
maybe this method below could help you in your calculations, it returns an integer corresponding to the day of the year so if you apply this to your holidays days and compare to the days of interest it could be a good way to find matches.
here it is, just add these lines outside of any function in your script (so you can use it anywhere) then use it like this :
var d = new Date().getDOY();
Logger.log(d)
Here the method :
Date.prototype.getDOY = function() {
var onejan = new Date(this.getFullYear(),0,1);
return Math.ceil((this - onejan) / 86400000);
}
Assuming that lastSundayDate is being passed around correctly, I see a glaring problem:
lastSundayDate.valueOf().
valueOf() on Date objects returns the primitive value... it looks like you're going for adding a day to the date (86400 seconds * j)? I can't tell what the logic is supposed to be here. But the valueOf() date2 is definitely giving you an integer something like: 1384628769399 (see here).
What you really want to accomplish is something like Date.getDay(), or something similar so that you can add hours, days, etc. to the original Date. This is likely the source of all your problems.
What you can do is read the Mozilla Developer Network documentation on Date objects to see all of the functions on Dates and their uses. You can greatly simplify what you're trying to do by using these functions, instead of doing abstract operations like j * 86400.
It should also be noted that you can do simple operations such as the following, to add 4 hours to the current Date (time):
var myDate = new Date();
Logger.log(myDate); // ~ console.write
var laterDate = new Date(myDate.setHours(myDate.getHours() + 4));
Logger.log(laterDate); // ~ console.write
which gives the following:
[13-11-16 14:13:38:947 EST] Sat Nov 16 14:13:38 GMT-05:00 2013
[13-11-16 14:13:38:954 EST] Sat Nov 16 18:13:38 GMT-05:00 2013
Working with dates can be tricky - but it's always best to use the simplest methods that are available, which are built into the Date objects themselves. There are also numerous other libraries that provide extended functionality for Dates such as Date js.
If you're still running into your problem after attempting to try using methods I displayed above, please run your script and post both the Execution Transcript and the content of the Logger so that I can help you narrow down the issue :)