本文介绍了从日,月和年生成NSDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 现在我的尝试是这样的:我试图从一个月和一年生成一个NSDate NSCalendar * calendar = [NSCalendar currentCalendar]; NSDateComponents * components = [[NSDateComponents alloc] init]; NSNumber * day = [dataSource valueForKey:@day]; NSNumber * month = [dataSource valueForKey:@month]; NSNumber * year = [dataSource valueForKey:@year]; [components setDay:[day intValue]]; [components setMonth:[month intValue]]; [components setMonth:[year intValue]]; NSDate * _date = [calendar dateFromComponents:components];但是,当day = 24,month = 8时, _date 会输出以下信息: 0168-07-25 04:56:02 +0000 我必须做一些可怕的错误,但我不知道什么。 解决方案您的代码中有错字:(最后一行应该是 setYear:) [components setDay:[day intValue] [components setMonth:[month intValue]]; [components setYear:[year intValue]]; I'm trying to generate a NSDate from a month day and year (all in integer format).Right now my attempt is such:NSCalendar *calendar = [NSCalendar currentCalendar];NSDateComponents *components = [[NSDateComponents alloc] init];NSNumber *day = [dataSource valueForKey:@"day"];NSNumber *month = [dataSource valueForKey:@"month"];NSNumber *year = [dataSource valueForKey:@"year"];[components setDay:[day intValue]];[components setMonth:[month intValue]];[components setMonth:[year intValue]];NSDate *_date = [calendar dateFromComponents:components];However, _date outputs the following when day = 24, month = 8, year = 2011: 0168-07-25 04:56:02 +0000I must be doing something horribly wrong, but I have no idea what. Anyone know what might be going on? 解决方案 You have a typo in your code: (last line should be setYear:) [components setDay:[day intValue]]; [components setMonth:[month intValue]]; [components setYear:[year intValue]]; 这篇关于从日,月和年生成NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-24 18:04