iOS 时间戳和标准时间的转换

#pragma mark--时间戳转保准时间
- (NSString *)timerWith:(NSString *)time
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyyMMdd"];
    NSString *str = [NSString stringWithFormat:@"%@",[formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:[time intValue]]]];
    return str;
}
//标准时间转时间戳
- (NSString *)change:(NSString *)timeStr
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyyMMdd"];
    NSDate* date1 = [formatter dateFromString:timeStr];
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date1 timeIntervalSince1970]];
    return timeSp;
}
 //获取当前时间
        NSDate *nowDate1 = [NSDate date];
        NSTimeZone *zone = [NSTimeZone systemTimeZone];
        NSInteger interval = [zone secondsFromGMTForDate:nowDate1];
        NSDate *nowDate = [nowDate1 dateByAddingTimeInterval:interval];
        NSLog(@"dat==%@",nowDate);
//两个时间之间的差
 - (int)intervalSinceNow: (NSString *) theDate
    {
        //过去的时间
        NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSDate *d=[formatter dateFromString:theDate];
        NSTimeInterval late=[d timeIntervalSince1970]*1;

        //获取当前时间(有时差)
        NSDate *nowDate1 = [NSDate date];
        NSTimeInterval now=[nowDate1 timeIntervalSince1970]*1;
        NSString *timeString=@"";

        NSTimeInterval cha=now-late;

        if (cha/86400>0)
        {
            timeString = [NSString stringWithFormat:@"%f", cha/86400];
            NSLog(@"timer1111==%@",timeString);
            timeString = [timeString substringToIndex:timeString.length-7];
            int timeInt = [timeString intValue]+1;
            NSLog(@"timer==%@",timeString);
            return timeInt;
        }    
        return 1;
    }
//获取N天后的时间
-(NSString *)getFutureDate:(NSInteger)day
{
    NSDate *nowDate = [NSDate date];
    //未来的时间
    NSDate *theDate;
    if (day!=0)
    {
        NSTimeInterval oneDay = 24*60*60*1; //一天的长度
        theDate = [nowDate initWithTimeIntervalSinceNow:oneDay*day]; //从现在往前后推的秒数
    }
    else
    {
        theDate = nowDate;
    }
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *futureDateStr = [formatter stringFromDate:theDate];
    return futureDateStr;
}
展开阅读全文

没有更多推荐了,返回首页