要在iOS7中解析的JSON的日期格式

时间:2014-05-02 07:09:43

标签: ios json date ios7 date-format

我想在iOS中解析以下JSON响应:

"myCaldDate": 1392076800000  

这种日期格式是什么?我们如何在iOS应用中以dd-mm-yyyy格式解析和显示,并将日期同步到设备日历?

谢谢

3 个答案:

答案 0 :(得分:0)

-(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;
}

现在同步: -

-(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];
}
}

答案 1 :(得分:0)

正如Crowder所说,它似乎是从UNIX date或Epoch开始的时间间隔。 要创建一个字符串日期,您应该:

  1. 从该时间间隔创建NSDate对象
  2. 创建并设置NSDateFormatter对象
  3. 从日期格式化程序
  4. 创建字符串

    在代码中:

    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:返回一个NSDate对象,该对象设置为1970年1月1日格林威治标准时间第一个瞬间的给定秒数。

         
        
    • (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)seconds参数seconds从参考日期开始的秒数,1   1970年1月,GMT,为新的日期。使用否定参数   指定此日期之前的日期。返回值NSDate对象设置为   从参考日期开始的秒数。
    •   
         

    讨论此方法对于从中创建NSDate对象很有用   BSD系统函数返回的time_t值。

答案 2 :(得分:0)

 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);