Unix时间戳 和 NSDate 的转换

时间:2023-02-11 02:37:41

 

一个时间戳字符串:NSString *timeStampStr =  @"1441602721";

转换成时间           

      

       double unixTimeStamp               = [timeStampStr doubleValue];
            NSTimeInterval timeInterval     = unixTimeStamp;
            NSDate *date                      = [NSDate dateWithTimeIntervalSince1970:timeInterval];
            NSDateFormatter *dateformatter   = [[NSDateFormatter alloc]init];
            [dateformatter setLocale:[NSLocale currentLocale]];
            [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            NSString *dateString          = [dateformatter stringFromDate:date];
            NSLog(@"dateString                  = %@", dateString);;

 

    //毫秒需要除以1000;

            double timestampval          = [timeStampStr doubleValue]/1000;
           NSTimeInterval timestamp      = (NSTimeInterval)timestampval;
            NSDate *updatetimestamp        = [NSDate dateWithTimeIntervalSince1970:timestamp];
            NSLog(@"updatetimestamp       = %@", updatetimestamp);
            //NSDate *dateTraded               = [NSDate dateWithTimeIntervalSince1970 :[timeStampStr integerValue]];
            //NSLog(@"dateTraded               = %@", dateTraded);


       NSDate 转换成时间戳:

   NSDate *date       = [NSDate date];

   NSTimeInterval ti   = [date timeIntervalSince1970];