Convert NSDate to JSON Date?

I need to send an object NSDateusing an HTTP message on parse.com.
I do not 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];

Parameters are serialized in the body of the Http request. I know that everything else I do is correct, because if I comment on the addition NSDateto the parameters, then it works.

+4
source share
3 answers

try it.

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];
+4
source

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

+3
source

Parse.com :

{
    "__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];
+1

Source: https://habr.com/ru/post/1621681/


All Articles