[NSCFString timeIntervalSinceReferenceDate]: unrecognized selector sent to instance - ios

I would like to compare difference between two dates, but with the code below I got the error "[NSCFString timeIntervalSinceReferenceDate]: unrecognized selector sent to instance." What's wrong with the code?
NSDate *dateAdded=[eventDictionary objectForKey:#"dateAdded"];
NSDate *validUntilDate=[eventDictionary objectForKey:#"validUntilDate"];
NSDateComponents *sometimeAgo = [[NSCalendar currentCalendar] components:(NSSecondCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:dateAdded toDate:validUntilDate options:0];

It looks like dateAdded and/or validUntilDate are actually strings and not dates. Maybe they are strings representing dates, but strings after all.

You need to use an NSDateFormatter to convert your date strings to actual NSDates.

Related

how to make call to 2 numbers depending om the time

sorry for all the questions im new to iOS but im wondering is it possible to set a time frame in iOS for example if user push button call me between the time 6am - 6pm the dialer opens and makes the phone call to a nr 567895432 but if it is after hours for example between 6,01pm - 7,59am
if button call me pushed the user would call 89780324, I know this is possible on php if I should create a Webview app but this is a native app and I don't really want to access a data base just a simple 2way app that sends requests,
thank you in advance
If I understood it correctly,On button tap, you want to call two different numbers based on time.
Why don't you check on button press
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];
NSInteger currentHour = [components hour];
NSInteger currentMinute = [components minute];
NSInteger currentSecond = [components second];
if ((currentHour > 6) && (currentHour < 18)) {
call 567895432
}
else{
call 89780324
}
In swift 3.0
var components: DateComponents? =
Calendar.current.dateComponents(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit, from: Date())
var currentHour: Int? = components?.hour
var currentMinute: Int? = components?.minute
var currentSecond: Int? = components?.second
if (currentHour > 6) && (currentHour < 18) {
call
567895432
}
else {
call
89780324
}

Calculate duration between two dates(finding upcoming/old birthdays) in ios [duplicate]

This question already has answers here:
Number of days between two NSDates [duplicate]
(16 answers)
Closed 6 years ago.
How can i calculate duration between two dates. like
I am getting contacts birthday list from contacts framework. Now i want to compare Today date with user DOB value and i should display the "remaining days count"(birthday).
For Example:
In contacts i added one user like: DOB: dd/MM/yyyy -->12/04/1960
now today date is: 25/04/2016
I want to get the upcoming birthday date(duration between two dates)
o/p: 18 days
How can i get this
Try below line of code. you will get the solution.
NSDateComponents *components;
NSInteger days;
components = [[NSCalendar currentCalendar] components: NSDayCalendarUnit
fromDate: startDate toDate: endDate options: 0];
days = [components day];
Use the following
unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;
NSDateComponents *conversionInfo = [currCalendar components:unitFlags fromDate:fistDate toDate:secondDate options:0];
int months = [conversionInfo month]; // to get number of months
int days = [conversionInfo day]; // to get number of days
int hours = [conversionInfo hour]; // to get hour difference
int minutes = [conversionInfo minute]; // to get hour difference
copied from Difference between two NSDates

iOS: Value of NSCalendarUnitWeekday is 1 for a Monday

I am trying to parse out the day of the week given a NSDate.
NSDateComponents *comps = [[NSDateComponents alloc] init];
comps=[calendar components: NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitWeekOfMonth | NSCalendarUnitWeekOfYear | NSCalendarUnitWeekday fromDate:date];
When the value of date is 2015-11-30 00:58:52 +0000 (this is Monday), the value of comps.weekday is printed out as 1
When the value of date as 2015-11-15 09:59:46 +0000 (this is Sunday), the value of comps.weekday is also 1.
NSDateComponents uses the timezone of the NSCalendar to interpret the current day. NSCalendar defaults to the using the timezone of the device.
2015-11-30 00:58:52 UTC is only a Monday if you are in the UTC timezone, or one of the later ones. If you are in the EST (-5) timezone, then this would be a Sunday.
If you want to interpret the day of week at the UTC timezone then change the timeZone property to [NSTimezone timeZoneForSecondsFromGMT:0].
Documentation ref: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateComponents_Class/#//apple_ref/occ/instp/NSDateComponents/timeZone

Get NSDate from start of 7 days ago

I'm trying to do some simple 7 day history plotting. Given the current NSDate.date, I want to get the NSDate that corresponds to the start of day, 7 days ago. So basically 7 days prior to 0.00 this morning.
What I've tried, is the following:
// decompose the current date, do I need more component fields?
NSDateComponents *comps = [NSCalendar.currentCalendar components: NSDayCalendarUnit fromDate: NSDate.date];
NSLog(#"components: %#", comps);
// Back day up 7 days. Will this wrap appropriate across month/year boundaries?
comps.day -= 7;
NSDate *origin = comps.date;
NSLog(#"new date: %#", origin);
What I assumed was that by just specifying NSDayCalendarUnit, the other things would be defaults (like start of day, etc). Unfortunately, origin ends up as (null). What is the correct way to do this?
To construct a new date you should know not only a day but also a month and a year. So you should add NSYearCalendarUnit | NSMonthCalendarUnit. Also you should setup calendar and probably timezone properties of NSDateComponents's instance:
NSDateComponents *comps = [NSCalendar.currentCalendar components: NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate: NSDate.date];
comps.calendar = NSCalendar.currentCalendar;
comps.timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
NSLog(#"components: %#", comps);
// Back day up 7 days. Will this wrap appropriate across month/year boundaries?
comps.day -= 7;
NSDate *origin = comps.date;
NSLog(#"new date: %#", origin);

Convert month int value to formatted string

I want to use the "LLL" string format of NSDateFormatter to get the proper string for the month name: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec
All I have is the integer value of the month. For example, if the value is 5, then I need an output string of "May".
I don't want to use a switch. Is there any way I can use NSDateFormatter and apply setDateFormat: with "LLL" ?
A better way to do this is to get the monthSymbols array from your formatter and index into that:
NSString * monthName = [formatter monthSymbols][monthInt];
If you want a date, though, NSCalendar and NSDateComponents will get you there, letting you create an NSDate from whatever date elements you have.
NSCalendar * cal = [NSCalendar currentCalendar];
NSDateComponents * comps = [NSDateComponents new];
[comps setMonth:monthVal];
NSDate * monthOnlyDate = [cal dateFromComponents:comps];
Now you can use your formatter on the date.
(Note that this date is basically meaningless aside from its use for this particular purpose; any unset properties -- hours, day, year -- of the components will be undefined, and the calendar will use its own choice of defaults for their values.)

Resources