[cpp] 1.NSDateFormatter配合NSDate與NSString之間的轉化 NSDateFormatter有下面2個方法: - (NSString *)stringFromDate:(NSDate *)date;//NSDate轉NSString - (NSDate *)dateFromString:(NSString *)string;//NSString轉NSDate e.g. NSString *dateString=@"1900-01-01"; NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"yyyy-MM-dd"]; NSDate *date=[dateFormatter dateFromString:dateString]; [dateFormatter release]; NSString轉NSDate與上面類似 NSString *dateString=[dateFormatter dateFromString:[NSDate date]]; 2.NSDateFormatter 的一些格式介紹 [dateFormatter setDateFormat:@"yyyy年MM月dd日#EEEE"];EEEE為星期幾,EEE為周幾 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; [dateFormatter setDateFormat:@"yyyy年MMMMd日"];//MMMM 為xx月,一個d可以省去01日前的0 3.NSString轉NSDate少一天的解決辦法 用下面這種格式化方式 www.2cto.com 1.[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 2. NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:[NSDate date]]; NSDate *todayDate = [calendar dateFromComponents:components];