I have a string with the date as xx.yy.2020 from a series of measurements during the year. I want to get an NSDate object and from that weekday and day of the year. My code:
// setting parameters for testing:
tag = 1;
monat = 1;
jahr = 2020;
// Calendar object: TimeZone is Europe/Zurich.
NSCalendar *tagcalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[tagcalendar setTimeZone:[NSTimeZone localTimeZone]];
[tagcalendar setLocale:[NSLocale currentLocale]];
// Setting components with tag, monat and jahr from the measurement:
NSDateComponents *tagcomponents = [[NSDateComponents alloc] init];
tagcalendar.firstWeekday = 2;
[tagcomponents setDay:tag];
[tagcomponents setMonth:monat];
[tagcomponents setYear:jahr];
// Getting the Date:
NSDate *tagdatum = [tagcalendar dateFromComponents:tagcomponents];
// Getting day:
NSDateComponents *weekdayComponents = [tagcalendar components:(NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitMonth) fromDate:tagdatum];
NSInteger Tag = [weekdayComponents day];
// Getting weekday:
NSUInteger TagDerWoche = [tagcalendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfYear forDate:tagdatum];
// Getting day of year:
NSUInteger TagDesJahres = [tagcalendar ordinalityOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitYear forDate:tagdatum];
NSLog(@"tagdatum: %@ Tag: %d TagDerWoche: %d TagDesJahres: %d",tagdatum,Tag,TagDerWoche,TagDesJahres);
// control:
NSString* zeitzone = [[tagcalendar timeZone]abbreviation];
NSString* lokal = [[tagcalendar locale]countryCode];
NSLog(@"zeitzone: %@ lokal: %@",zeitzone, lokal);
With NSLog, tagdatum is correct, 01.01.2020.
I wonder why, in the Variables View,
tagdatum __NSTaggedDate * 2019-12-31 23:00:00 UTC
shows the previous day.