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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章