objective c - Get the Week Number in iOS SDK -
i want week number of year date.i tried code follow gives me wrong week number.
my code week number:
nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; [dateformatter setdateformat:@"yyyy-mm-dd"]; nsdate *date = [dateformatter datefromstring:@"2012-09-15"]; nscalendar *calendar = [nscalendar currentcalendar]; nslog(@"week: %i", [[calendar components: nsweekofyearcalendarunit fromdate:date] weekofyear]); //display 38 instead of 37 }
note: if try with [nsdate date]
display correct.
help me solve this.. thank you,
objectivec:
nscalendar *calendar = [nscalendar currentcalendar]; nsdatecomponents *datecomponent = [calendar components:(nsweekofyearcalendarunit | nsdaycalendarunit | nsmonthcalendarunit | nsyearcalendarunit) fromdate:[nsdate date]]; nslog(@"%@",datecomponent);
swift:
let calendar = nscalendar.currentcalendar() let datecomponent = calendar.components([.weekofyear, .day, .month, .year], fromdate: nsdate(timeintervalsincenow: 0)) print("weekofyear \(datecomponent.weekofyear)")
swift 3:
let component = calendar.current.component(.weekofyear, from: date()) print("week of year \(component)")
Comments
Post a Comment