I want to get the last weekday of a given month.
I've come across this simple answer on how to get the 1st/2nd/... weekday which works well.
The question is: How to get the last weekday of a given month?
Not every month has only 4 Sundays, so do I have to count the number of Sundays of the month, or is there a more elegant way to do this?
Had the same need recently, and the best I could come up with is the following, which is meant to be run every day to check if the current day is the last weekday of the current month:
<?php
$d = new DateObject('first day of this month', date_default_timezone());
$d->modify("+15 days");
$d->modify("first day of next month -1 weekday");
$last = date_format($d, 'd');
$today = new DateObject('today', date_default_timezone());
$today = date_format($today, 'd');
if ($today == $last) {
//bingo
}
?>
I have been testing and so far couldn't find an example where this fails. The reason for doing the modify("+15 days") in the middle is to be sure we are calling "next month" with a starting date that is not in the edge between two months, case where I believe this could fail.
Leaving the code shown before apparently covers all cases.
I finally came up with the following solution. I'm using NSDate-Extensions for convenience. dayOfWeek stands for Sunday (1) till Saturday (7) in the Gregorian calendar:
- (NSDate *)dateOfLastDayOfWeek:(NSInteger)dayOfWeek afterDate:(NSDate *)date
{
// Determine the date one month after the given date
date = [date dateByAddingMonths:1];
// Set the first day of this month
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.year = date.year;
dateComponents.month = date.month;
dateComponents.day = 1;
// Get the date and then the weekday of this first day of the month
NSDate *tempDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *firstDayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:tempDate];
NSInteger weekday = firstDayComponents.weekday;
// Determine how many days we have to go back to the desired weekday
NSInteger daysBeforeThe1stOfNextMonth = (weekday + 7) - dayOfWeek;
if (daysBeforeThe1stOfNextMonth > 7)
{
daysBeforeThe1stOfNextMonth -= 7;
}
NSDate *dateOfLastDayOfWeek = [tempDate dateBySubtractingDays:daysBeforeThe1stOfNextMonth];
return dateOfLastDayOfWeek;
}
Related
i'm not sure if this is possible but i can't find any clue of doing this with multidatespicker..
i'm creating a web booking system which need to allow customer to select any booking date which they like.
But now my client was requesting to create package like weekly package(4 times per month), biweekly(1 times per 2 weeks) and etc..For weekly example.. when customer choose tuesday of that week.. the rest of 6 days all will be disable.. and for biweekly.. for that week and next week will be disable..
Thousand appreciate for someone who could help :)
I have the same issue: in my booking project the user must can select days range but also week range (only from Saturday to Saturday).
I managed the selection of the week on onSelect() event of multiDatesPicker() init function: by click on a day of the week MultiDatesPicker auto-select the relative week from Saturday to Saturday.
Unfortunately, for me the open issue is the hover event, because I want MultiDatesPicker also hover the week by pass hover the days of a week, not only select it, so the user could have perception to really choose weeks, not days.
It's singular that MultiDatesPicker has not a onHover() function to manage the "pre-selection"/hover event.
var autoselectRange = [0, days_range];
$('.multidatespicker-wrapper').multiDatesPicker({
numberOfMonths: [1, 2],
minDate: 1,
firstDay: 1,
mode: 'daysRange',
onSelect: function(dateText, inst) {
var selectedDate = $.datepicker.parseDate($.datepicker._defaults.dateFormat, dateText);
from_date = new Date(moment(dateText, "MM/DD/YYYY").format("YYYY-MM-DD"));
to_date = new Date (moment(dateText, "MM/DD/YYYY").add('days', days_range - 1).format("YYYY-MM-DD"));
if (weekRange) {
// Setting of Start and End of the week
if (from_date.getDay() == 6) numDaysToWeekStart = 0; // because Saturday in this case it is Start of the week
else numDaysToWeekStart = from_date.getDay() + 1;
from_date = new Date(from_date.setDate(from_date.getDate() - numDaysToWeekStart));
to_date = new Date(to_date.setDate(to_date.getDate() - numDaysToWeekStart));
// Setting of the days of the week
date = new Date(from_date);
weeks = [];
while (date <= to_date) {
weeks.push(new Date(date));
date.setDate(date.getDate() + 1);
}
// Selection of the days of the week/weeks in MDP Calendar
$(this).multiDatesPicker('resetDates', 'picked');
$(this).multiDatesPicker('addDates', weeks);
}
// Any more controls
},
autoselectRange: autoselectRange,
pickableRange: days_range,
// any more settings });
I hope It could be a help; if you have some idea to manage hover event and you can share it. Thanks
I am trying to create a date for next Sunday, and make it repeat for every Sunday after.
I know how to create the date using components, and components setWeek is depreciated, so I don't know how to do it now.
if You got the First Sunday Then You Should Do this To repeat it after seven Days!
NSString *sunday = #"Here Your Sunday Date";
NSDate *dateSunday = [self dateFromString:sunday];
[self GetNextSunday:dateSunday];
Then Call This Method To Get Next Sunday
-(void)GetNextSunday:(NSDate*)date
{
NSDate *NextSunday = [[NSDate alloc]init];
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [gregorian components:( NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
components.day = components.day + 8;
NextSunday = [gregorian dateFromComponents:components];
NSLog(#"%#",NextSunday);
}
This Will Return Next 7th Day ! I hope this Will Help You.
for Any Query Comment Below!
I want to fire a UILocalNotification every day at 6:00, 9:00, 12:00 for the next 20 days.
I realize that this question is a very basic question, I would try to accomplish this with NSDateComponents, but then I realized that I could get problems when a month just has 28 days or the year changes. Thats why I ask: Has someone experience with such a task and could give me some hints?
Create three different local notification with repeatInterval.
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = date; // Specifly date and time for notification
localNotification.repeatInterval = NSCalendarUnitDay;
localNotification.alertBody = #"Notification";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
At time of 20 days cancel those notification.
[[UIApplication sharedApplication] cancelAllLocalNotifications];
or
[[UIApplication sharedApplication] cancelLocalNotification:NOTIFICATION_ID];
I have a label I want to update daily and automatically? in some specific time. How could the code be.. I have searched every where about how to updating the label daily.
the NSdate and NStimer label working . And also datepicker if the user want to look at forward events.
I think it is something with, 'if' the date is.. }else{... and something like that
Thanks
edit:
If I already have these codes in my app then there will be something wrong
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
NSDate *nowDate = [NSDate date];
self.currentDateLabel.text = [dateFormatter stringFromDate:nowDate];
Try this code for any comparison between dates... You should not compare date in the form of string. Compare the dates before conversion to string. Convert the self.serverDate into date format using dateFromString function of the formatter by specifying the exact date format as that of the dateString. Then compare the dates using following function.
-(void) callAfterSixtySecond:(NSTimer*) t
{
NSDate *today = [NSDate date]; // current date
NSDate *newDate = self.serverDate; // other date
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"HH:mm"];
NSDate *todayTime = [formatter dateFromString:[formatter stringFromDate:today]];
NSDate *requiredTime = [formatter dateFromString:[formatter stringFromDate:newDate]];
NSComparisonResult result;
result = [todayTime compare:requiredTime ]; // comparing two dates
if(result == NSOrderedAscending)
NSLog(#"today is less");
else if(result == NSOrderedDescending)
NSLog(#"newDate is less");
else if(result == NSOrderedSame)
NSLog(#"Both dates are same"); // time has reached. Update Label using setText method of label
else
NSLog(#"Date cannot be compared");
}
You will need to run this method every minute using an NSTimer...
NSTimer* myTimer = [NSTimer scheduledTimerWithTimeInterval: 60.0 target: self
selector: #selector(callAfterSixtySecond:) userInfo: nil repeats: YES];
I am using NSDateFormatter to convert a series of dates to a week number within a month.
The date formatting code I am using is yyyyMMW and everything I have read tells me that W will be between 1-5.
But, the 2nd of June 2013 fell on a Sunday (the default start day of the week in the gregorian calendar) and it's week number is reported as 0 even though the start date of the week is calculated correctly:
2013-06-03 14:15:45.611 date=20130531, week=2013055, start of week=20130526
2013-06-03 14:15:45.612 date=20130602, week=2013060, start of week=20130602
2013-06-03 14:15:45.612 date=20130603, week=2013061, start of week=20130602
Some quick and dirty test code to reproduce the log shown above:
NSDateFormatter *dateFormatDaily = [[NSDateFormatter alloc] init];
[dateFormatDaily setDateFormat:#"yyyyMMdd"];
NSDateFormatter *dateFormatterWeekly = [[NSDateFormatter alloc] init];
[dateFormatterWeekly setDateFormat:#"yyyyMMW"];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setFirstWeekday:1]; // default but set here for clarity
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:5];
[dateComponents setDay:31];
[dateComponents setYear:2013];
NSDate *date_1 = [calendar dateFromComponents:dateComponents];
[dateComponents setMonth:6];
[dateComponents setDay:2];
NSDate *date_2 = [calendar dateFromComponents:dateComponents];
[dateComponents setDay:3];
NSDate *date_3 = [calendar dateFromComponents:dateComponents];
NSArray *datesToTest = #[date_1, date_2, date_3];
for (NSDate *date in datesToTest) {
NSString *weekNo = [dateFormatterWeekly stringFromDate:date];
NSDate *beginningOfWeek = nil;
BOOL rc = [calendar rangeOfUnit:NSWeekCalendarUnit startDate:&beginningOfWeek interval:NULL forDate:date];
if (rc) {
NSLog(#"date=%#, week=%#, start of week=%#", [dateFormatDaily stringFromDate:date], weekNo, [dateFormatDaily stringFromDate:beginningOfWeek]);
} else {
NSLog(#"Could not calculate beginning of week");
}
}
Any ideas? A week number of 0 under any circumstances seems wrong to me.
Thanks
There are various parameters that cause this effect. First of all, you did not set a calendar for the date formatter. If you add
[dateFormatterWeekly setCalendar:calendar];
to your code, then the output will be as you expected:
date=20130531, week=2013055, start of week=20130526
date=20130602, week=2013062, start of week=20130602
date=20130603, week=2013062, start of week=20130602
But in your case, the date formatter uses the current calendar, and therefore has separate parameters firstWeekDay and minimumDaysInFirstWeek. These parameters are locale dependent. If I test this on the iOS Simulator with the "Region Format" set to "German -> Germany", then
[[dateFormatterWeekly calendar] firstWeekday] = 2
[[dateFormatterWeekly calendar] minimumDaysInFirstWeek] = 4
and I assume that you will have similar values, because now I get the same output as you.
Now for the date formatter, the week starts on a Monday, which means that June 2 is in the week starting at May 27. This counts as "week #0" in June, because only one day of this week is in June, but minimumDaysInFirstWeek = 4. The first week in a month that has at least
minimumDaysInFirstWeek days, counts as "week #1".
(I found the relevance of the minimumDaysInFirstWeek parameter here:
http://www.cocoabuilder.com/archive/cocoa/326845-week-of-month-confusion.html)