same day of last month ios - ios

Is there any way using NSCalender you can get same day of last month?
I am using a calender which shows user 1 month like the iPad calender when they click on the button I want to move to previous month but should select the same day as before.
I want to just do
[components setMonth:([components month] - 1)];
but this will create problems when I are moving from a month with 31 days to month with 30 days and selected day is 31st.
I was able to find examples for android but not iOS.
android example
Any help would be appreciated

Check out this link.
It's about adding one month, but you could probably do the same with subtracting.
Change
[dateComponents setMonth:1];
into
[dateComponents setMonth:-1];

Seems there is no obvious "right" answer and no "built-in" answer.
As Chris's "simple" idea may lead to invalid dates, you may have to handle the edge-cases.
Pseudocode to deal with day-month-year:
if month = December start with day-1-(year-1), else
day-(month-1)-year [using dateComponents]
check if this a valid date (using NSDateFormatter like in this question
repeat subtracting one day until you reach a valid date
Another idea:
prevMonthDate = startDate;
Repeat
prevMonthDate = prevMonthDate - 1 day
Until (Month(prevMonthDate) < Month(startDate) Or Year(prevMonthDate) < Year(startDate))
And (Day(prevMonthDate) <= Day(startDate))
This requires working with NSDate and NSDateComponents, check out the Date and Time Programming Guide.

Related

Why does ordinalityOfUnit use Monday as the first day of the week when using NSCalendarUnitEra?

I'm trying to calculate the number of calendar weeks between two dates. I'm using the following code:
[cal ordinalityOfUnit:NSCalendarUnitWeekOfYear inUnit:NSCalendarUnitEra forDate:thenDate];
[cal ordinalityOfUnit:NSCalendarUnitWeekOfYear inUnit:NSCalendarUnitEra forDate:nowDate];
A date of 15-1-2017, a Sunday, results in 105192 weeks, and 16-1-2017, a Monday, results in 105193. That would indicate it is using Monday as the first day of the week. I have verified that cal.firstWeekday is 1. If I change the inUnit from NSCalendarUnitEra to NSCalendarUnitYear it works correctly. Is there a way around this?
I suppose I could subtract one day from both dates but that seems very hackie.
After much experimenting, if you make sure all dates are in GMT and you set cal timeZone to GMT it works as one would expect.

How do I get a label in Xcode to show the day of the week (Swift/Swift 2) [duplicate]

This question already has answers here:
Displaying the Day Of The Week From Date Picker
(2 answers)
Closed 7 years ago.
I'm an absolutely coding newb and I want to create a school planner app. I have the general nuts and bolts sorted - but I want to create a function where a label displays the day of the week, which a button would then interpret and would then send the user to the appropriate View Controller with that day's timetable in.
I've tried YouTube and of course here, and I can make no sense of it at all. Can someone treat me like a little baby and explain it to me. The name of the label is DayLabel1 and I think I can connect it as an IBOutlet.
I make no sense out of this whatsoever, and if you can't help me, I am either
a) doomed
b) still doomed
This is not a duplicate as I'm not wanting a date picker. I'm a newb and just want a hand.
Thank you! :-)
[[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:[NSDate date]] weekday];
This code gives you to number of day in week which start from Sunday (index of Sunday is 0). You can make an String enum to get string using this index.
Here is the Swift:
let calendar = NSCalendar.currentCalendar()
let date = NSDate()
let dateComponent = calendar.components(NSCalendarUnit.CalendarUnitWeekday, fromDate: date)
let weekday = dateComponent.weekday

Get current date past midnight, Objective C

I want to get the current date, even if the time has passed midnight. Imagine it's friday night the 6th of June 2014 - we check the date Saturday at 2 am, but we still want this to count as being friday. How would I go about this?
Let's just say we cut it at 9am the next day. I.e. we will assume previous date until the time has passed 9 am. Yes, this is software used at a nightclub, as you can imagine.
I guess this would involve something like subtracting 1 day from the current date if the hour is less than 10?
You can use interval to specific date like
[[NSDate date] dateByAddingTimeInterval:interval];
Assuming the cutoff is 9am, all you need to do is create an NSDate that's 9 hours earlier than the actual time.
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:-(9 * 3600)];
That way every day begins and ends at 9am.

Compute number of weeks since epoch more accurately than days/7

I'm putting together a game where there's a tournament every week, and every week there's a different special bonus for the game.
To make this work I need to know which week it is so I can select the right bonus, and make sure the score goes to the right tournament.
A trivial answer is to take the number of days since epoch, offset to get to a monday, then compute the number of days and divide by 7. Obviously this fails because of leap year.
Another option would be to figure out which week of the year you're on, but that gets weird when you transition from one year to the next. Also, the tournament ends at the end of the day on Sunday, so it doesn't follow the normal week borders.
I was about to start doing some fairly complicated stuff using the year, day of year and day of week to try to figure it out, but I thought I'd ask here in case there was an easy solution I was missing.
This will be done in Objective-C on iOS.
This should work:
// Choose any reference date which is a Monday:
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *refComp = [[NSDateComponents alloc] init];
refComp.year = 1970;
refComp.month = 1;
refComp.day = 5;
NSDate *refDate = [cal dateFromComponents:refComp];
// Compute number of weeks between your date and the reference date:
NSDateComponents *comp = [cal components:NSCalendarUnitWeekOfYear fromDate:refDate toDate:yourDate options:0];
NSInteger weeks = comp.weekOfYear;
But calculating the number of days (since some Monday) and dividing by 7 should
give the same result because every week has 7 days, regardless of leap years.

iPhone OS4.3: NSCalendar's [ordinalityOfUnit:inUnit:forDate:] method returns different/wrong result

I've following code snippet which I'm using to deduce the first day of the week, to display in my calendar view. This code has been working without any issue, till I tested it on iPhone OS 4.3 onwards.
int firstDOW = [m_calendar ordinalityOfUnit:NSWeekdayCalendarUnit inUnit:NSWeekCalendarUnit forDate:fom]%7;
where 'fom' is the first date of the month.
When debugged the code, on iOS prior to 4.3, return value for 'ordinality' method seems to return correct value( for example if the 'fom' date falls on Friday, the value returned from above method is '5'). But on iOS >= 4.3, the return value is somehow not the correct weekday( for example, if the 'fom' date falls on Friday, the value returned from above method is '6'!).
I don't understand, whether there is any issue with my code, or it really is a bug in the above method.
Has anyone else faced this in iOS >= 4.3??
Thanks and Regards.
I learned a bit more about this method call. It appears that the ordinalityOfUnit:inUnit:forDate: method pays attention to what current locale is and what that locale considers the first day of the week to be.
For example. In the United States locale the first day of the week is considered to be Sunday while in the Ireland locale, the first day of the week is considered to be Monday.
If you are in the US locale, and ask for the ordinality of Tuesday, it will return 2 (0 for Sunday, 1 for Monday and 2 for Tuesday). In the Ireland locale, however, it will return 1 (0 for Monday and 1 for Tuesday).
In short, the ordinality of the day within the week depends on which day is considered to begin the week. In contrast, if you use the NSDateComponents API and get the day of the week from there, you always get a day relative to Sunday === 0. e.g.
NSDateComponents *components = [[self calendar] components: NSWeekdayCalendarUnit fromDate: [self startingDay]];
NSUInteger weekdayIndex = [components weekday];

Resources