How to create an NSDate date object?

前端 未结 3 1556
迷失自我
迷失自我 2021-02-05 10:26

How can I create an NSDate from the day, month and year? There don\'t seem to be any methods to do this and they have removed the class method dateWithString<

3条回答
  •  无人共我
    2021-02-05 10:41

    You can use NSDateComponents:

    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setDay:6];
    [comps setMonth:5];
    [comps setYear:2004];
    NSCalendar *gregorian = [[NSCalendar alloc]
        initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate *date = [gregorian dateFromComponents:comps];
    [comps release];
    

提交回复
热议问题