nscalendar

iOS one month from current Date.

妖精的绣舞 提交于 2019-12-01 06:39:07
I'm trying to get one month from the current date. Below is what I have and it is returning: 4027-09-24 16:59:00 +0000 . currentDate is right. What's going on? // Get todays date to set the monthly subscription expiration date NSDate *currentDate = [NSDate date]; NSLog(@"Current Date = %@", currentDate); NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit fromDate:currentDate]; dateComponents.month = dateComponents.month + 1; NSDate *currentDatePlus1Month = [[NSCalendar currentCalendar] dateByAddingComponents

iPhone NSDate eg. next Friday

人走茶凉 提交于 2019-12-01 06:18:54
问题 I want to create a function which results the date of next Friday but I have no plan how to do it. Has anyone a good hint to me ? 回答1: E.g. Get current date using NSDate, then use 'components>fromDate:' from NSCalendar to get the NSDateComponents, then add the time difference to next Friday and create a new NSDate and Bob's is your uncle. 回答2: Here is my working solution for getting the next 5 Sundays in Gregorian calendar: self.nextBeginDates = [NSMutableArray array]; NSDateComponents

NSCalendar dateFromComponents: GMT timezone instead of systemTimeZone

倾然丶 夕夏残阳落幕 提交于 2019-12-01 05:26:40
This code NSCalendar *calendar =[NSCalendar currentCalendar]; [gregorian setTimeZone:tz]; NSLog(@"%@", [gregorian timeZone]); NSDateComponents *comp = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:[NSDate date]]; [comp setDay:info.day]; [comp setMonth:info.month]; [comp setYear:info.year]; [comp setHour:info.hour]; [comp setMinute:info.minute]; [comp setSecond:info.second]; [comp setTimeZone:tz]; NSLog(@"%@", [comp timeZone]); NSLog(@"%@", [gregorian dateFromComponents:comp]); shows the following in console: Europe/Moscow (MSKS) offset 14400 (Daylight) Europe/Moscow

format a NSDate to DDMMYYYY?

徘徊边缘 提交于 2019-12-01 04:27:49
问题 I have to send a DDMMYYY date from a Date to communicate with an API. NSDateComponents *dateComponents; NSCalendar *gregorian; NSDate *date; gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; dateComponents = [[NSDateComponents alloc] init]; self.date = [gregorian dateByAddingComponents:dateComponents toDate:[NSDate date] options:0]; Do I have to use the [dateComponents day] and so on? is there some method that can help me to performing that task like in PHP? 回答1

Get first day in a month from NSCalendar

五迷三道 提交于 2019-12-01 03:46:51
I have this subset of a method that needs to get day one of the current month. NSDate *today = [NSDate date]; // returns correctly 28 february 2013 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [[NSDateComponents alloc] init]; components.day = 1; NSDate *dayOneInCurrentMonth = [gregorian dateByAddingComponents:components toDate:today options:0]; dayOneInCurrentMonth then prints out 2013-03-01 09:53:49 +0000 , the first day of the next month. How do I get day one of the current month? Your logic is wrong: Instead of

Cocoa get first day in week

萝らか妹 提交于 2019-12-01 01:39:38
How to get the first day of a week for a date this seems more easy at it is, since : when the week starts with sunday, i need to get back the sunday date if it starts on monday, i need to get the monday date the input date is any date in week with time... i tried several approaches, but the edge case make it difficould i made a function, which however doesn't work in 100% (not sure about the [components setDay: -weekday + 2];) - (NSDate *)firstDateOfWeek { NSCalendar * calendar = [NSCalendar currentCalendar]; NSDateComponents *weekdayComponents = [calendar components:(NSDayCalendarUnit |

Schedule UILocalNotification everyday at specific time

人走茶凉 提交于 2019-11-30 15:24:41
I am using below piece of code to schedule a localnotification at specific time. But the notification is repeating for every minute instead of after one day. Whether i have missed any notification settings. My timezone is (Asia/Calcutta (IST) offset 19800) and using iPhone 4s. - (void)applicationDidEnterBackground:(UIApplication *)application { NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ; NSDate *now = [NSDate date]; NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute)

NSDate for first day of the month

折月煮酒 提交于 2019-11-30 07:58:15
This is quite a simple concept, but as of yet I have been unable to find an elegant (and calendar locale independent) solution. I need to find the first day of the month for an arbitrary NSDate . For example, given an arbitrary NSDate ( arbitraryDate ) another NSDate object will be returned (let's call this firstDayOfMonthDate ) which represents the first day of the month in arbitraryDate . The time component does not matter, as I just need the NSDate object for the first day of the month (although for neatness it would be useful if the time was just zeroed out). Thanks in advance for any

How to get NSDate for 00:00 on the day of [NSDate date]?

一曲冷凌霜 提交于 2019-11-30 06:57:09
问题 I need to get an NSDate object for 00:00(beginning of the day) from [NSDate date], let's say if currently it is 11:30am(returned by [NSDate date]), on 01/06/2012, now I need to have an NSDate for 00:00am on 01/06/2012. I haven't tried this, but what is in my mind is: NSDate *now = [NSDate date]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit |

Schedule UILocalNotification everyday at specific time

孤街浪徒 提交于 2019-11-29 22:34:18
问题 I am using below piece of code to schedule a localnotification at specific time. But the notification is repeating for every minute instead of after one day. Whether i have missed any notification settings. My timezone is (Asia/Calcutta (IST) offset 19800) and using iPhone 4s. - (void)applicationDidEnterBackground:(UIApplication *)application { NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ; NSDate *now = [NSDate date]; NSDateComponents *components = [calendar components: