Last active
May 16, 2016 09:23
-
-
Save mr5z/62a9c8525afd2dabd329f8ad820415f8 to your computer and use it in GitHub Desktop.
Get first day of the current week as NSDate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)test_printDateVariants { | |
NSDateFormatter *fmt = [NSDateFormatter new]; | |
NSTimeZone *timezone = [NSTimeZone timeZoneWithName:@"UTC"]; | |
[fmt setTimeZone:timezone]; | |
[fmt setDateFormat:@"yyyy-MM-dd hh:mm:ss Z"]; | |
NSCalendar *calendar = [NSCalendar currentCalendar]; | |
for(int i = 1;i <= 31; ++i) { | |
[fmt setDateFormat:@"yyyy-MM-dd hh:mm:ss Z"]; | |
NSDate *date = [fmt dateFromString:[NSString stringWithFormat:@"2016-05-%02d 10:08:24 +0800", i]]; | |
NSInteger firstdayOfWeek = [calendar ordinalityOfUnit:NSWeekdayCalendarUnit inUnit:NSWeekCalendarUnit forDate:date] - 1; | |
NSDate *newDate = [NSDate dateWithTimeInterval:(-firstdayOfWeek * (60 * 60 * 24)) sinceDate:date]; | |
[fmt setDateFormat:@"dd"]; | |
NSLog(@"date: %@, firstdayOfWeek: %lu", [fmt stringFromDate:newDate], firstdayOfWeek); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment