4

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.

oguz ismail
  • 1
  • 16
  • 47
  • 69
WilliaGuy
  • 111
  • 2
  • 7

3 Answers3

4

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];
iOS Developer
  • 138
  • 2
  • 18
pradip kikani
  • 627
  • 6
  • 14
3

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

Denis Krivitski
  • 654
  • 8
  • 11
  • 1
    I recommend that you do _not_ use ISO8601DateFormatter, as it fails when the time has milliseconds. http://www.openradar.me/29609526 – adam-p Jun 07 '18 at 16:59
  • After almost 3 years and the radar is open ! – joan Oct 19 '18 at 12:43
1

According to the Parse.com documentation:

{
    "__type": "Date",
    "iso": "2011-08-21T18:02:52.249Z"
}

Obj-C

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSString *newString = [dateFormat stringFromDate:date];
[params setObject:newString forKey:kKeyDate];
nschum
  • 15,322
  • 5
  • 58
  • 56
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179