Skip to content

Instantly share code, notes, and snippets.

@HarrisHan
Created January 11, 2017 08:11
Show Gist options
  • Save HarrisHan/a71b7584d10adf6478fb8c4749128574 to your computer and use it in GitHub Desktop.
Save HarrisHan/a71b7584d10adf6478fb8c4749128574 to your computer and use it in GitHub Desktop.
beginningOfDay and endOfDay
+ (NSDate *)beginningOfDay:(NSDate *)date;
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ) fromDate:date];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
return [cal dateFromComponents:components];
}
+ (NSDate *)endOfDay:(NSDate *)date;
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ) fromDate:date];
[components setHour:23];
[components setMinute:59];
[components setSecond:59];
return [cal dateFromComponents:components];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment