Get first day in a month from NSCalendar

前端 未结 6 1323
醉话见心
醉话见心 2021-01-12 05:24

I have this subset of a method that needs to get day one of the current month.

NSDate *today = [NSDate date];  // returns correctly 28 february 2013
NSCalend         


        
6条回答
  •  花落未央
    2021-01-12 05:42

    By creating new variable is expensive on memory usage, it's better using date formatter to get the first day of the current month

    today = [NSDate date];
    
    firstDayDateFormatter = [[NSDateFormatter alloc] init];
    [firstDayDateFormatter setDateFormat:@"01-MM-yyyy"];
    dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    [dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US"]];
    firstDayOfTheMonth = [mdfDateFormat dateFromString:[firstDayDateFormatter stringFromDate:today]];
    

提交回复
热议问题