1. 程式人生 > >iOS開發-時間戳s的轉換

iOS開發-時間戳s的轉換

// 轉換成當前時間 yyyy-MM-dd HH:mm:ss

- (NSString *)timeFormatted:(int)totalSeconds

{

    NSDate *date = [NSDate dateWithTimeIntervalSince1970:totalSeconds];

NSTimeZone *zone = [NSTimeZonesystemTimeZone];

    NSInteger interval = [zone secondsFromGMTForDate: date];

    NSDate *localeDate = [date  dateByAddingTimeInterval

: interval];

NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];

    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSString *currentDateString = [dateFormatter stringFromDate:localeDate];

    return currentDateString;

}