nsdatecomponents

Compare two NSDates for same date/time [duplicate]

十年热恋 提交于 2019-12-17 18:37:47
问题 This question already has answers here : How to compare two dates in Objective-C (14 answers) Closed 6 years ago . Is date1 == date2 not a valid way to compare? If not, what is the correct alternative? Here's my code: - (NSDate*) dateWithNoTime { unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; NSCalendar* calendar = [NSCalendar currentCalendar]; NSDateComponents* components = [calendar components:flags fromDate:self]; NSDate* dateOnly = [calendar

NSDate beginning of day and end of day

僤鯓⒐⒋嵵緔 提交于 2019-12-17 07:13:25
问题 -(NSDate *)beginningOfDay:(NSDate *)date { NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *components = [cal components:( NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:date]; [components setHour:0]; [components setMinute:0]; [components setSecond:0]; return [cal dateFromComponents:components]; } -(NSDate *)endOfDay:(NSDate *)date { NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents

NSDateComponents on IOS6 not working?

↘锁芯ラ 提交于 2019-12-14 04:22:49
问题 I have a simple program which was working with IOS4 and 5. Now with IOS 6 I'm getting the following error message: 2012-10-05 11:08:16.386 app[1698:19d03] 2012-10-05 09:08:11 +0000 2012-10-05 11:08:18.072 app[1698:19d03] 2012-10-05 09:08:11 +0000 2012-10-05 11:08:19.273 app[1698:19d03] 2012-10-05 09:08:11 +0000 2012-10-05 11:08:19.274 app[1698:19d03] 2012-10-05 09:08:11 +0000 2012-10-05 11:08:19.275 app[1698:19d03] 0 2012-10-05 11:08:25.055 app[1698:19d03] 2011-10-05 09:08:11 +0000 2012-10-05

How to get NSDateComponents between two NSDateComponents?

徘徊边缘 提交于 2019-12-14 03:36:48
问题 I've two NSDateComponents , I want all NSDateComponents in between those two, I've tried the following, NSDateComponents *first = ...; NSDateComponents *second = ...; BOOL boolDone = NO; while (!boolDone) { [array addObject:first]; first.day+=1; NSLog(@"%@",first); if([[first date] compare:[second date]] == NSOrderedSame) { boolDone = YES; } } NSLog(@"All dates : %@",array); After the loop, it just prints the date I've in first NSDateComponent ...!! What's wrong? Here's a log to understand

Issue with NSDate from NSDateComponents

怎甘沉沦 提交于 2019-12-14 03:28:56
问题 i have a string like 2013-03-05T07:37:26.853 and i want to get the Output : 2013-03-05 07:37:26 . I want the final output in NSDate object. i am using below function to convert desire output. and it returning wrong time. - (NSDate *) getDateFromCustomString:(NSString *) dateStr { NSArray *dateStrParts = [dateStr componentsSeparatedByString:@"T"]; NSString *datePart = [dateStrParts objectAtIndex:0]; NSString *timePart = [dateStrParts objectAtIndex:1]; NSArray *dateParts = [datePart

NSCalendar Find Saturday and Sunday of Current Month

家住魔仙堡 提交于 2019-12-13 21:03:08
问题 I am working on project where I want to integrate NSCalendar functionality in my app. Hence I googled and I found this solution. It give me all months in the year and also I can select particular date. that's fine. But I wanted to disable all saturdays and sundays of every month somehow I tried to do it but when I tap previous month or next month it changes the selection. I have gone through almost all stackoverflow questions But I didn't find proper way to do it. I request you to all Is

How would I save an object of type DateComponents to a CloudKit field?

情到浓时终转凉″ 提交于 2019-12-13 17:47:28
问题 I am using Swift for an iOS app. I need to store a DateComponents object as a field in CloudKit. How would I do that? So far I think I need to set the CloudKit field to type Bytes, and convert the DateComponents object to a Data object. Looking at the documentation for the Data class, I can't figure out how to initialize the Data object. I have no idea how to use the UnsafeBufferPointer used in one of the initializers. The following code gives a runtime error: newRecord.setObject(

Converting can NSString to NSDate format in objective C [duplicate]

泪湿孤枕 提交于 2019-12-13 09:46:19
问题 This question already has answers here : Convert string to date in my iPhone app (4 answers) issue with date formatting using NSDateFormatter (3 answers) Closed 6 years ago . How can i convert the following string "October 24,Monday 12:30 AM EST" to NSDateFormat. i tried this code NSDateFormatter *dateformat=[[NSDateFormatter alloc]init]; [dateformat setDateFormat:@"MM/DD/YYYY hh:mm a"]; NSDate *datefor=[dateformat dateFromString:appDelegate.appoinmentString]; [dateformat setDateFormat:@"MM

NSDate early by 1 hour?

断了今生、忘了曾经 提交于 2019-12-13 08:25:53
问题 I am using the following code to set a date object: NSDate *date = [NSDate date]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar]; NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: date]; [components setHour: 18]; [components setMinute: 00]; [components setSecond: 00]; reminderDate = [gregorian dateFromComponents: components]; However, when I NSLog reminderDate I get the following output: Time: 2012-08-29 17:00:00 +0000

Calculate time elapsed between pressing the same button twice

本秂侑毒 提交于 2019-12-13 07:17:29
问题 I've tried all the time calculating examples I found on this site but somehow I'm getting 0 as value every time. I'm new to IOS and the NSDate is giving me a run for it's money :) I want to record time A when I press button "startStop", and then record time B when I press it again. Pressing it a second time (deselecting) has to calculate the time elapsed between these 2 dates. So far I have this: -(IBAction)buttonClick { NSDate *startStopDate = [NSDate alloc]; NSDateFormatter *formatter= [