// NSData *nowDate = [[NSDate alloc]init]; NSDate *nowDate2 = [NSDate date]; NSLog(@"%@",nowDate2); //1970年到當前時間的時間戳 NSTimeInterval time1970 = [nowDate2 timeIntervalSince1970]; NSLog(@"%lf",time1970);
//若是代碼測試過,日期時間和你的不同,你要設置時區(北京) //獲取全部時區的名字 NSArray *arr = [NSTimeZone knownTimeZoneNames]; NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"GMT"]; NSLog(@"%@",arr); NSDate *nowDate = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"YYYY-MM-DD HH-mm-ss"];//設置格式化 日期格式 zz表明時區 [dateFormatter setTimeZone:timeZone];//設置格式化時區 NSString *timeformatter = [dateFormatter stringFromDate:nowDate]; NSLog(@"%@",timeformatter); NSString *str = @"2014年06月21日 19點25分04秒"; NSDateFormatter *timeFormatter = [[NSDateFormatter alloc]init]; [timeFormatter setDateFormat:@"YYYY年MM月DD日 HH點mm分ss秒"]; NSData *date = [timeFormatter dateFromString:str]; NSLog(@"%@",date);