How to create an NSDate date object?

前端 未结 3 1548
迷失自我
迷失自我 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:31

    A slightly different answer to those already posted; if you have a fixed string format you'd like to use to create dates then you can use something like:

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    dateFormatter.locale = [NSLocale localeWithIdentifier:@"en_US_POSIX"]; 
        // see QA1480; NSDateFormatter otherwise reserves the right slightly to
        // modify any date string passed to it, according to user settings, per
        // it's other use, for UI work
    
    dateFormatter.dateFormat = @"dd MMM yyyy"; 
        // or whatever you want; per the unicode standards
    
    NSDate *dateFromString = [dateFormatter dateFromString:stringContainingDate];
    

提交回复
热议问题