//1.時間格式 NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init]; [_dateFormatter setDateFormat:@"YYYY年MM月dd天hh小時mm分ss秒"]; //2.獲取系統時間 NSString *date = [_dateFormatter stringFromDate:[NSDate date]]; //3.NSDate轉化為時間戳 NSTimeInterval a=[date timeIntervalSince1970]; //4.時間戳轉化為NSDate NSDate *stampDate = [NSDate dateWithTimeIntervalSince1970:timeInter]; //4.NSDate轉化為NSString NSString* dateString = [_dateFormatter stringFromDate:date]; //5.傳入時間差,重置格式 //傳入時間差,轉換格式 - (NSString *)timeWithMessageString:(NSTimeInterval)timeInter { int month = timeInter / (3600 * 24 * 30); int day = timeInter / (3600 * 24); int hour = timeInter / 3600; int minute = timeInter / 60; int day_process = day - month * 30; int hour_process = hour - day *24; int minute_process = minute - hour *60; int miao_process = timeInter - minute*60; NSString *timedate = nil; if (day == 0) { timedate = [NSString stringWithFormat:@"%d小時%d分%d秒",hour_process,minute_process,miao_process]; if (hour == 0) { timedate = [NSString stringWithFormat:@"%d分%d秒",minute_process,miao_process]; if (hour == 0) { timedate = [NSString stringWithFormat:@"%d秒",miao_process]; } } }else{ timedate = [NSString stringWithFormat:@"%d天%d小時%d分%d秒",day_process,hour_process,minute_process,miao_process]; } timedate = [NSString stringWithFormat:@"限時優惠: 還剩%@",timedate]; return timedate; }
//當前時間的半小時后 NSDate *date1 = [NSDate date]; NSDate *date2 = [[NSDate date] dateByAddingTimeInterval: 30*60]; NSDate *date3 = [NSDate dateWithTimeIntervalSinceNow:30*60];