0

I have a timestamp as below. Its in a string format "/Date(1402987019190+0000)/" I would like to convert it into NSDate. Can somebody help me into this one?

TechnoGeezer
  • 197
  • 2
  • 2
  • 14

2 Answers2

1

Use dateWithTimeIntervalSince1970:,

NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp];
NSLog(@"%@", date);

Use this function to convert string to timestamp Ref. https://stackoverflow.com/a/932130/1868660

-(NSString *)dateDiff:(NSString *)origDate {
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setFormatterBehavior:NSDateFormatterBehavior10_4];
    [df setDateFormat:@"EEE, dd MMM yy HH:mm:ss VVVV"];
    NSDate *convertedDate = [df dateFromString:origDate];
    [df release];
    NSDate *todayDate = [NSDate date];
    double ti = [convertedDate timeIntervalSinceDate:todayDate];
    ti = ti * -1;
    if(ti < 1) {
        return @"never";
    } else  if (ti < 60) {
        return @"less than a minute ago";
    } else if (ti < 3600) {
        int diff = round(ti / 60);
        return [NSString stringWithFormat:@"%d minutes ago", diff];
    } else if (ti < 86400) {
        int diff = round(ti / 60 / 60);
        return[NSString stringWithFormat:@"%d hours ago", diff];
    } else if (ti < 2629743) {
        int diff = round(ti / 60 / 60 / 24);
        return[NSString stringWithFormat:@"%d days ago", diff];
    } else {
        return @"never";
    }   
}
Community
  • 1
  • 1
Subodh Ghulaxe
  • 18,333
  • 14
  • 83
  • 102
1

If I correctly understood what you mean, you need to parse timestamp from the string with given format. You can use regular expression to extract timestamp value.

NSString * timestampString = @"/Date(1402987019190+0000)/";
NSString *pattern = @"/Date\\(([0-9]{1,})\\+0000\\)/";

NSRegularExpression *regex = [NSRegularExpression
 regularExpressionWithPattern:pattern
                      options:NSRegularExpressionCaseInsensitive
                        error:nil];
NSTextCheckingResult *textCheckingResult = [regex firstMatchInString:timestampString options:0 range:NSMakeRange(0, timestampString.length)];
NSRange matchRange = [textCheckingResult rangeAtIndex:1];
NSString *match = [timestampString substringWithRange:matchRange];

NSDate *date = [NSDate dateWithTimeIntervalSince1970:[match intValue];
NSLog(@"%@", date);
Alexander Tkachenko
  • 3,221
  • 1
  • 33
  • 47
  • Hi Alex, Your understanding was just perfect. But the thing is original date is 06/17/2014 12:06:59 and after conversion when we print it through NSLog i get this 2038-01-19 03:14:07 +0000. – TechnoGeezer Jun 17 '14 at 08:07
  • Oh.. I think maybe your timestamp is not in seconds? Amey, do you know what time unit is in your timestamp? Maybe your timestamp is not from standard date (1970)? – Alexander Tkachenko Jun 17 '14 at 08:18
  • The thing is that Unix timestamp of your date (06/17/2014 12:06:59) is 1403006819 You may check it here http://www.unixtimestamp.com/index.php – Alexander Tkachenko Jun 17 '14 at 08:22
  • The response says, its a JSON date format, it does not have as such unit. So in that case, is there any way to get date from this timestamp? – TechnoGeezer Jun 17 '14 at 08:39
  • You need to know what your timestamp means. It seems that it's not standard. – Alexander Tkachenko Jun 17 '14 at 08:50
  • Hi Alex, Finally i get it, the timestamp was in milliseconds so i had to divide it first by 1000 and then apply your code. Thanks for help. – TechnoGeezer Jun 17 '14 at 09:31