Amirul Amri
Amirul Amri

Reputation: 13

How to store date into parse database(iOS)

I'm trying to insert date and time from two separate textfield into parse.com database, I manage to combined them into one NSString but when creating a NSDate to be stored into the database it came out as nil.

NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'"];
NSString *dateTime = ([NSString stringWithFormat:@"%@T%@",jobDateTF.text,jobTimeTF.text]);
NSDate *dateNTime = [formatter dateFromString:dateTime];

And for passing into the database,

advanceBooking[@"jobDate"] = dateNTime;

Forgive me for any poor formatting, new to stackoverflow

Upvotes: 1

Views: 176

Answers (2)

Gaurav Gudaliya
Gaurav Gudaliya

Reputation: 131

NSDate *date = [datePicker date];

// format the NSDate to a NSString
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"cccc,MMM d,hh:mm aa"];
NSString *dateString = [dateFormat stringFromDate:date];

// save to Parse
PFObject *addValues= [PFObject objectWithClassName:@"your-class"];
[addValues setObject: dateString forKey:@"your-key"];
[addValues saveInBackground];

Upvotes: 0

TheEye
TheEye

Reputation: 9346

Your DateFormatter specifies year-month-day before the time, but your time string does not contain year, month and day - so the dateNTime is probably nil. Have a look at NSDateComponents for your date construction.

EDIT: With the new information from your comment:

When you want to get a date from a string, you have to specify the EXACT format - your format string said the date part should look like '2015-12-10', but your string said '10-12-2015'. How should the date formatter know what to do with your string? Please read up on NSDateFormatter and the possible format strings, there is also lots of info on that here on StackOverflow.

Upvotes: 1

Related Questions