UIDatePicker Not storing time in core data - xcode

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.

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).

NSDatePicker Error

Ever time I try to make a string in xcode it won't work can someone point out what's wrong and help please
NSString *date = self.DatePicker.date;
This is the error "Incompatible pointer types initializing 'NSString * strong' with an expression of type 'NSDate *'"
self.DatePicker.date; returns date.
You are storing it in NSString.
Use:
NSDate *date = self.DatePicker.date;
Then if you want to store it in string then use:
NSString *dateString = [NSDateFormatter localizedStringFromDate:[NSDate date]
dateStyle:NSDateFormatterShortStyle
timeStyle:NSDateFormatterFullStyle];

Date to String conversion (NSDateFormatter) doesn't work

I'm working on an iOS project, retrieving data from SQLite through JSON. The table has a datetime column called 'date'. When I retrieve the date in Xcode and printout the result, I see "2012-09-02T16:30:00Z". But when I try to convert it using NSDateFormatter, the result is null.
NSString *formatString = [NSDateFormatter dateFormatFromTemplate:#"EdMMM" options:0 locale:[NSLocale currentLocale]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:formatString];
NSString *dateString = [dateFormatter stringFromDate:categoryAtIndex.date];
NSLog(#"%# => %#", categoryAtIndex.date, dateString);
The above outputs:
2012-09-02T16:30:00Z => (null)
in stead of a nicely formatted datestring.
Any thoughts?
Thanks to mask8 I found the issue was related to another part of my code. I found this post on stackoverflow to solve the way to change the format of the date that I retrieved through JSON. I also found another post on stackoverflow describing how to handle conversions from a string to NSDate and vice versa.

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];

How to display NSTimeInterval binding as NSDate in InterfaceBuilder?

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.

Resources