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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值