NSDate *today = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+2"]];
NSDateComponents *dateComponents = [calendar components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];
NSInteger day = [dateComponents day];
NSInteger month = [dateComponents month];
NSInteger year = [dateComponents year];
NSLog(@" %i %i %i ", day, month, year);
That code show me "12 2147483647 2147483647"
How can I get the month and year (integer)
How can add/forward one day? (if we are the first of the month too!)
Thant you for your attention :-)
NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDate *date = [NSDate date];
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];
NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];
NSInteger day = [dateComponents day];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];
[calendar release];
hope this helps
Add more units to NSDayCalendarUnit | NSWeekdayCalendarUnit
- you are not requesting month or year, therefore they are invalid!
Add/subtract a NSDateComponents
object configured for 1 day and [NSCalendar dateByAddingComponents:toDate:options:]
. Alternatively, and probably just as correct, use [NSDate dateWithTimeInterval:sinceDate:]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With