How to display NSTimeInterval binding as NSDate in InterfaceBuilder? - interface-builder

I handle a lot of time stamps that I store as NSTimeInterval.
NSDateComponents* comps = [[NSDateComponents alloc] init];
[comps setYear: ....
NSDate* date = [gregorian dateFromComponents:comps];
NSTimeInterval timeStamp = [date timeIntervalSinceReferenceDate];
Interface Builder has a table view binding to the variable of the time stamp.
How can I format the (double value of) NSTimeInterval into a human readable date format?
The Interface Builder Table Column Bindings palette has a input field Value Transformer - can I do something here?

The trick is to drag and drop NSDateFormatter from the Library panel to the table view cell.

Related

Set up custom time to allow NSLocalNotif xcode

See sample code as follows - unable to use custom time here. Why is this not possible giving a result:
NSDate *dt1 = [NSDate date]; //2018-07-26 08:31:22 +0000
NSString *datestr = [NSString stringWithFormat:#"%#", [NSDate date]];//2018-07-26 10:42:09 +0001
NSString *cutstring = 09:00 PM;
I would just create date with NSDateComponents where you can set year, month, hour, minute etc. Refer https://nshipster.com/nsdatecomponents/
Extracting Components From Dates
NSDateComponents can be initialized and manipulated manually, but most often, they’re extracted from a specified date, using NSCalendar -components:fromDate::
Swift
Objective-C
let calendar = NSCalendar.currentCalendar()
let date = NSDate()
let components = calendar.components([.Month, .Day], fromDate: date)
The components parameter is a bitmask of the date component values to retrieve, with many to choose from:
Swift
Objective-C
NSCalendarUnit.Era
NSCalendarUnit.Year
NSCalendarUnit.Month
NSCalendarUnit.Day
NSCalendarUnit.Hour
NSCalendarUnit.Minute
NSCalendarUnit.Second
NSCalendarUnit.Weekday
NSCalendarUnit.WeekdayOrdinal
NSCalendarUnit.Quarter
NSCalendarUnit.WeekOfMonth
NSCalendarUnit.WeekOfYear
NSCalendarUnit.YearForWeekOfYear
NSCalendarUnit.Calendar
NSCalendarUnit.TimeZone
Since it would be expensive to compute all of the possible values, specify only the components that will be used in subsequent calculations (joining with |, the bitwise OR operator).

cocoa : assign nstimeInterval value to nsdate?

I want to assign nstimeInterval value to nsdate.
NSDate *startTime=[NSDate date] ;
NSDate *endTime=[NSDate date] ;
NSTimeInterval difference=[endTime
timeIntervalSinceDate:startTime];
startTime=difference; This gives me an error.
how can i assign my difference result of nsTimeInterval type to nsdate?
thanks in Advance
You need to understand that NSTimeInterval is the difference between two dates. What you're trying to do is like telling the program to:
Set the date to 2 days.
When what you want to do is tell the program to:
Set the date to last monday plus 2 days.
Therefore you need to use one of these family of NSDate methods:
+ dateWithTimeIntervalSinceNow:
+ dateWithTimeInterval:sinceDate:
+ dateWithTimeIntervalSinceReferenceDate:
+ dateWithTimeIntervalSince1970:
– initWithTimeIntervalSinceNow:
– initWithTimeInterval:sinceDate:
– initWithTimeIntervalSinceReferenceDate:
– initWithTimeIntervalSince1970:
Where you create the NSDate with some reference date plus/minus the NSTimeInterval; note that NSDate objects are immutable so there are no methods to change the date represented by the object using NSTimeInterval values.
See the NSDate Class Reference for more details.
To correct your code, this is how you'd do it:
NSDate *startTime=[NSDate date] ;
NSDate *endTime=[NSDate date] ;
NSTimeInterval difference=[endTime timeIntervalSinceDate:startTime];
NSDate *newTime = [NSDate dateWithTimeInterval:difference sinceDate:startDate];

Time comparison in Cocoa

I am storing the date and time in strings using NSUserDefaults when an action is performed. next time the app is run, I want to check the time since the last date and time, and display a message if this is greater than a specified time period.
Is this possible?
In viewDidLoad I retrieve the NSUserDefaults with the saved date and time, and I get the current date, but how do i compare them, and 'do something' if the time difference is bigger than specified?
Use NSDate's timeIntervalSinceDate: method.
By the way, you can store NSDates directly in NSUserDefaults. You don't need to convert them to/from strings.
You could do something like that:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDate * lastUpdate = [defaults valueForKey:#"mysettingdate"];
if(!lastUpdate){
lastUpdate = [NSDate dateWithTimeIntervalSince1970:0];
}
NSDate *localDate = [NSDate date];
NSTimeInterval timeZoneOffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
NSTimeInterval gmtTimeInterval = [localDate timeIntervalSinceReferenceDate] + timeZoneOffset;
NSDate *gmtDate = [NSDate dateWithTimeIntervalSinceReferenceDate:gmtTimeInterval];
NSDateComponents *diff = [[NSCalendar currentCalendar] components:NSMinuteCalendarUnit fromDate:lastUpdate toDate:gmtDate options:0];
NSLog(#"diff in minutes:%d", [diff minute]); //or hour, days, seconds
if ([diff minute]>0) {
//do your stuff :)
}

UIDatePicker Not storing time in core data

I am using a UIDatepicker with only "Time", I save the time in a NSDate object type, however when I try to store the Object in core data I get an error saying its not a NSDate type...
tt = [pickerTime date];
[myObject setValue:tt forKey:#"time"];
Thanks,
Hey, a see a mistake here:
tt = [pickerTime date];
tt's type is either an NSDate, or... You have to declare what tt is:
NSDate *tt = [pickerTime date];
Make sure that your using a date field in IB.

Calculate time difference in Cocoa

I have got two timevalues in the format: %H%M%S (E.G.161500)
These values are text-based integers.
Is there a simple function in Cocoa that calculates the difference between these two integers using a 60 seconds and minutes scale?
So if
time 1 = 161500
time 2 = 171500
timedifference = 003000
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"HHmmss"];
NSDate *date1 = [dateFormatter dateFromString:#"161500"];
NSDate *date2 = [dateFormatter dateFromString:#"171500"];
NSTimeInterval diff = [date2 timeIntervalSinceDate:date1]; // diff = 3600.0
The class for manipulating dates is NSDate. The method for getting time intervals is -timeIntervalSinceDate:. The result is a NSTimeInterval value, which is a double representing the interval in seconds.
You can create a NSDate object from a NSString with +dateWithString:, provided that your date is formatted as 2001-03-24 10:45:32 +0600.
try this code.
- (NSTimeInterval)intervalBetweenDate:(NSDate *)dt1 andDate:(NSDate *)dt2 {
NSTimeInterval interval = [dt2 timeIntervalSinceDate:dt1];
NSLog(#"%f",interval);
return interval;
}
I would create an NSFormatter subclass to parse time values in that format from input data (you can put one on a text field to automatically convert user input, or use it to parse from a data source in code). Have it return the combined number of seconds as an NSTimeInterval (double representing seconds) wrapped in an NSNumber. From there it's easy to subtract the difference, and display it using the same NSFormatter class you created. In both parsing and displaying values, you're the one responsible to write code converting from seconds to hours:minutes:seconds or whatever format you like. You could also convert these values to an NSDate like mouviciel mentioned, if it makes sense for your application. Just keep in mind you're always going to be storing the time difference from a reference date, usually Jan 1st 1970 (NSDate has methods to do this for you).

Resources