How can I initialize a date to NSdate?

前端 未结 3 1294
你的背包
你的背包 2021-01-11 20:10

I want to give, for example, 12/11/2005 in the format of mm/dd/yyyy. Can I initialize 12/11/2005 to NSDate directly? Any

3条回答
  •  渐次进展
    2021-01-11 20:42

    Another solution could be the use of NSCalendar and NSDateComponents, since +dateWithNaturalLanguageString: is deprecated.

    For example you want a NSDate initialised to 15th of August 2001. This is the code that will work for you:

    NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    comps.year = 2001;
    comps.month = 8;
    comps.day = 15;
    
    NSDate *myDate = [g dateFromComponents:comps];
    

    or another way is:

    let earliestDate = calendar.dateWithEra(1, year: 2012, month: 1, day: 1, hour: 0, minute: 0, second: 0, nanosecond: 0)
    

    to set the earliestDate NSDate to 01/01/2012

提交回复
热议问题