iOS 本地時間 / UTC時間 / 時間戳記等操作 / 擷取當前年月日,iosutc
//獲得目前時間並且轉為字串
- (NSString *)dateTransformToTimeString{ NSDate *currentDate = [NSDate date];//獲得目前時間為UTC時間 2014-07-16 07:54:36 UTC (UTC時間比標準時間差8小時) //轉為字串 NSDateFormatter*df = [[NSDateFormatter alloc]init];//執行個體化時間格式類 [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//格式化 //2014-07-16 07:54:36(NSString類) NSString *timeString = [df stringFromDate:currentDate]; return timeString;}
//擷取目前時間轉為時間戳記
- (NSString *)dateTransformToTimeSp{ UInt64 recordTime = [[NSDate date] timeIntervalSince1970]*1000;//用戶端當前13位毫秒級時間戳記 NSString *timeSp = [NSString stringWithFormat:@"%llu",recordTime];//時間戳記轉字串(13位毫秒級時間戳記字串) return timeSp;}
1 //時間戳記字串1469193006001(毫秒)1469193006.001(毫秒,1469193006001234(微秒)1469193006.001234(微秒)轉 UTC時間2016-08-11T07:00:55.611Z 2 - (NSString *)timespToUTCFormat:(NSString *)timesp 3 { 4 NSString *timeString = [timesp stringByReplacingOccurrencesOfString:@"." withString:@""]; 5 if (timeString.length >= 10) { 6 NSString *second = [timeString substringToIndex:10]; 7 NSString *milliscond = [timeString substringFromIndex:10]; 8 NSString * timeStampString = [NSString stringWithFormat:@"%@.%@",second,milliscond]; 9 NSTimeInterval _interval=[timeStampString doubleValue];10 NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];11 12 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];13 NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];14 [dateFormatter setTimeZone:timeZone];15 [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];16 NSString *dateString = [dateFormatter stringFromDate:date];17 18 return dateString;19 }20 return @"";21 }
//13位時間戳記1469193006001(毫秒)轉 系統時間2016-08-11 08:55:36
1 + (NSString *)timespToYMDFormat:(NSNumber *)timesp 2 { 3 NSString *stime = [timesp stringValue]; 4 NSTimeInterval time = [[stime substringToIndex:10] doubleValue]; 5 NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time]; 6 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 7 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 8 9 return [dateFormatter stringFromDate: detaildate];10 }
//時間轉時間戳記的方法:sendDate為NSDate類
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[sendDate timeIntervalSince1970]];
如果只擷取當前的年月日,用NSDate 直接截取是不對的,以下方法提供了擷取當前的年月日等等
// 擷取代表西曆的NSCalendar對象 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; // 擷取當前日期 // 定義一個時間欄位的旗標,指定將會擷取指定年、月、日、時、分、秒的資訊 unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday; // 擷取不同時間欄位的資訊 NSDateComponents* comp = [gregorian components: unitFlags fromDate:localeDate]; NSInteger year = comp.year;//下面是可以擷取的內容 //@property NSInteger era;@property NSInteger year;@property NSInteger month;@property NSInteger day;@property NSInteger hour;@property NSInteger minute;@property NSInteger second;@property NSInteger nanosecond NS_AVAILABLE(10_7, 5_0);@property NSInteger weekday;@property NSInteger weekdayOrdinal;@property NSInteger quarter NS_AVAILABLE(10_6, 4_0);@property NSInteger weekOfMonth NS_AVAILABLE(10_7, 5_0);@property NSInteger weekOfYear NS_AVAILABLE(10_7, 5_0);@property NSInteger yearForWeekOfYear NS_AVAILABLE(10_7, 5_0);