Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert NSDate to JSON Date?

I need to post a NSDate object using HTTP post to parse.com.
I don't know how to convert it.

//1  nsdate
[params setObject:date forKey:kKeyDate];

//2 nsstring
 NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
 [dateFormat setDateFormat:@"yyyy-MM-dd T HH : mm : ss.SSSZ"];
 NSString *newString = [dateFormat stringFromDate:date];
 [params setObject:newString forKey:kKeyDate];

Params are serialized in the body of the Http request. I know that everything else I'm doing is correct because if I comment out adding the NSDate to the params, then it works.

like image 652
WilliaGuy Avatar asked Feb 08 '23 05:02

WilliaGuy


2 Answers

try this ..

NSDate *CurrentDate = [NSDate date];
NSDateFormatter *DATEFORMATER = [[NSDateFormatter alloc]init];
[DATEFORMATER setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'"];
[DATEFORMATER setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSString *DATE_STRING = [DATEFORMATER stringFromDate:CurrentDate];
like image 138
pradip kikani Avatar answered Feb 10 '23 22:02

pradip kikani


If you are using iOS 10.0 or above, use the ISO8601DateFormatter class.

like image 43
Denis Krivitski Avatar answered Feb 10 '23 22:02

Denis Krivitski