Question

I would like to parse the following JSON response in iOS:

"myCaldDate": 1392076800000  

What is this date format called and how do we parse and display in dd-mm-yyyy format in iOS apps and sync the date to device calendar?

Thanks

Was it helpful?

Solution

-(NSDate)dateDiff:(double )myCaldDate
{
   / Date difference for the purpose of showing games listes
   double timeInterval = myCaldDate/1000.0f;
   NSDate *convertedDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];

   return convertedDate;
}

Now Sync :-

-(NSString *)syncWithCalender:(NSDate *)convertDate
{

NSDate *todayDate = [NSDate date];
double ti = [convertDate timeIntervalSinceDate:todayDate];
ti = ti * -1;
if(ti <= 1)
{
    return @"Just Now";
}
else if (ti < 60)
{
    return @"less than a minute ago";
}
else if (ti < 3600)
{
    int diff = (ti / 60);
    if (diff==1)
    {
        return [NSString stringWithFormat:@"%d minute ago", diff];
    }
    return [NSString stringWithFormat:@"%d minutes ago", diff];
}
else if (ti < 86400)
{
    int diff = (ti / 60 / 60);
    if (diff == 1)
    {
        return[NSString stringWithFormat:@"%d hour ago", diff];
    }
    return[NSString stringWithFormat:@"%d hours ago", diff];
}
else if (ti < 2629743)
{
    int diff = (ti / 60 / 60 / 24);
    if (diff == 1)
    {
        return[NSString stringWithFormat:@"%d day ago", diff];
    }
    return[NSString stringWithFormat:@"%d days ago", diff];
}
else if (ti < 31557600)
{
    int diff = (ti / 60 / 60 / 24 / 30);
    if (diff == 1)
    {
        return[NSString stringWithFormat:@"%d month ago", diff];
    }
    return[NSString stringWithFormat:@"%d months ago", diff];
}
else
{
    int diff = (ti / 60 / 60 / 24 / 30 / 12);
    if (diff == 1)
    {
        return[NSString stringWithFormat:@"%d year ago", diff];
    }
    return[NSString stringWithFormat:@"%d years ago", diff];
}
}

OTHER TIPS

It seems to be the time interval from UNIX date or Epoch as Crowder said. To create a string date from that you should:

  1. Create an NSDate object from that interval
  2. Create and set an NSDateFormatter object
  3. Create the string from the date formatter

In code:

NSDate * date = [[NSDate alloc] initWithTimeIntervalSince1970:interval];
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
    //you can choose dofferetnt styles or create your own by using a format string conforms to uni specifications
    dateFormatter.dateStyle = NSDateFormatterMediumStyle;
    dateFormatter.timeStyle = NSDateFormatterNoStyle;
    NSString * string = [dateFormatter stringFromDate:date];

-initWithTimeIntervalSince1970: Returns an NSDate object set to the given number of seconds from the first instant of 1 January 1970, GMT.

  • (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)seconds Parameters seconds The number of seconds from the reference date, 1 January 1970, GMT, for the new date. Use a negative argument to specify a date before this date. Return Value An NSDate object set to seconds seconds from the reference date.

Discussion This method is useful for creating NSDate objects from time_t values returned by BSD system functions.

 NSDate *date = [NSDate dateWithTimeIntervalSince1970:[@"1392076800000" doubleValue]/1000];

NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"LLL dd yyyy, hh:mm a"];
NSString *strdate=[formatter stringFromDate:date];
NSLog(@"%@",strdate);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top