2013-12-11 137 views
0

我有在長長整型格式創建時間戳 EX的方法:1386752892如何正確生成時間戳

+ (NSNumber *)currentTimestampWithLongLongFormat 
{ 
    double timeStamp = ceil([[NSDate date] timeIntervalSince1970] * 1000); 

    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; 
    [formatter setGeneratesDecimalNumbers:false]; 
    NSNumber *timeNumber = [NSNumber numberWithDouble:timeStamp]; 
    NSString *timeString = [formatter stringFromNumber:timeNumber]; 

    // NSTimeInterval is defined as double 
    return [NSNumber numberWithLongLong:[timeString longLongValue]]; 
} 

但是,這將產生13個七段數 EX:1386752811802

如何解決問題並生成正確的數字格式?

回答

2
int timestamp = [[NSDate date] timeIntervalSince1970]; 
+1

我會用'NSTimeInterval''的'而不是''int''。這就是''timeIntervalSince1970''返回的結果,它是一個C''double''。 – amb

+0

:D,不明白你在試圖說什麼 –

0

試試這個

/** 
* @param nil 
* @return current time in mili second 
* 
* Fetch the current time stamp 
*/ 

-(NSString *)currentTimeStamp { 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 

NSTimeZone *gmt = [NSTimeZone timeZoneWithName:@"GMT"]; 
[dateFormatter setTimeZone:gmt]; 
NSString *timeStamp1 = [dateFormatter stringFromDate:[NSDate date]]; 
NSDate *curdate = [dateFormatter dateFromString:timeStamp1]; 

double unix_timestamp = [curdate timeIntervalSince1970]; 
NSString *timeStamp = [NSString stringWithFormat:@"%f",unix_timestamp*1000]; 
return timeStamp; 
} 
+0

對於任何人來說,NSDateFormatter應該謹慎使用,因爲它會減慢你的代碼速度。 – humblePilgrim

0
+ (NSString*) dateFromString:(NSString*)aStr 

{ 
    NSDateFormatter *formater = [[NSDateFormatter alloc] init]; 
    [formater setDateFormat:@"yyyy-MM-dd HH:mm"]; 
    NSDate *date2 = [formater dateFromString:aStr]; 
    [formater setDateFormat:@"d MMM,yyyy HH:mm"]; 
    NSString *result = [formater stringFromDate:date2]; 


    return result; 
} 

+ (NSString *)calculateTime:(NSString *)datetime :(NSString *)servertime 

{ 


    NSString *time; 

    NSDate *date1; 
    NSDate *date2; 
    { 
     NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
     [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
     date1 = [formatter dateFromString:datetime]; 
     date2 = [formatter dateFromString:servertime]; 

    } 


    NSCalendar *calendar = [NSCalendar currentCalendar]; 

    NSDateComponents *differenceComponents = [calendar components:(NSDayCalendarUnit) 
                 fromDate:date1 
                  toDate:date2 
                  options:0]; 

    NSTimeInterval interval = [date1 timeIntervalSinceDate: date2];//[date1 timeIntervalSince1970] - [date2 timeIntervalSince1970]; 
    int hour = interval/3600; 
    int minute = (int)interval % 3600/60; 
    int seconds = (int)interval % 60; 
    hour=ABS(hour); 
    minute=ABS(minute); 
    seconds=ABS(seconds); 
    if ([differenceComponents day]>0) { 
     time= [NSString stringWithFormat:@"%ld %@", (long)[differenceComponents day],[NSString stringWithFormat:NSLocalizedString(@"daysago", nil)]]; 
    } 
    else 
    { 
     if ([differenceComponents day] == 0) { 
      time= [NSString stringWithFormat:@"%ld %@", (long)[differenceComponents day],[NSString stringWithFormat:NSLocalizedString(@"dayago", nil)]]; 
      if (hour>0) { 
       time= [NSString stringWithFormat:@"%d %@", ABS(hour),[NSString stringWithFormat:NSLocalizedString(@"hourago", nil)]]; 
      } 
      else { 
       time= [NSString stringWithFormat:@"%d %@", ABS(hour),[NSString stringWithFormat:NSLocalizedString(@"hoursago", nil)]]; 

       if (minute>0) { 

        time= [NSString stringWithFormat:@"%d %@", ABS(minute),[NSString stringWithFormat:NSLocalizedString(@"minuteago", nil)]]; 
       } 
       else { 
        time= [NSString stringWithFormat:@"%d %@", ABS(minute),[NSString stringWithFormat:NSLocalizedString(@"minuteago", nil)]]; 
        if (seconds>0) { 
         time= [NSString stringWithFormat:@"%d %@", ABS(seconds),[NSString stringWithFormat:NSLocalizedString(@"secondago", nil)]]; 
        } 
        else { 
         time= [NSString stringWithFormat:@"%d %@", ABS(seconds),[NSString stringWithFormat:NSLocalizedString(@"secondsago", nil)]]; 
        } 

       } 
      } 

     } 

    } 

    return time; 
} 

///按規定,我們將使用日期格式