#pragma mark 日期字符串按照指定的格式转换成日期
+ (NSDate *)dateFromString:(NSString *)string withFormat:(NSString *)format {
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:format];
NSDate *date = [inputFormatter dateFromString:string];
return date;
}
#pragma mark 日期按照指定的格式转换成字符串
+ (NSString *)stringFromDate:(NSDate *)date withFormat:(NSString *)format {
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:format];
NSString *dateString = [outputFormatter stringFromDate:date];
return dateString;
}
#pragma mark 获取年
- (NSInteger)getYear {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dayComponents = [calendar components:(NSCalendarUnitYear) fromDate:self];
return [dayComponents year];
}
#pragma mark 获取月
- (NSInteger)getMonth {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dayComponents = [calendar components:(NSCalendarUnitMonth) fromDate:self];
return [dayComponents month];
}
#pragma mark 获取日
- (NSInteger)getDay {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dayComponents = [calendar components:(NSCalendarUnitDay) fromDate:self];
return [dayComponents day];
}
#pragma mark 获取时
- (NSInteger)getHour {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dayComponents = [calendar components:(NSCalendarUnitHour) fromDate:self];
return [dayComponents hour];
}
#pragma mark 获取分
- (NSInteger)getMinute {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dayComponents = [calendar components:(NSCalendarUnitMinute) fromDate:self];
return [dayComponents minute];
}
#pragma mark 获取秒
- (NSInteger)getSecond {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dayComponents = [calendar components:(NSCalendarUnitSecond) fromDate:self];
return [dayComponents second];
}
#pragma mark 获取当前月份的天数
+ (NSInteger)getNumberOfDaysInMonth {
NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSRange monthRange = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:[NSDate date]];
return monthRange.length;
}
#pragma mark 获取指定月份的天数
+ (NSInteger)getNumberOfDaysInMonthWithDateString:(NSString *)dateString {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
// 此处根据需求改对应的日期格式
[dateFormatter setDateFormat:@"yyyy-MM"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
// 通过该方法计算特定日期月份的天数
NSRange monthRange = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
return monthRange.length;
}
#pragma mark 获取指定日期为星期几
+ (NSString *)weekdayStringFromDate:(NSDate *)inputDate {
NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/BeiJing"];
[calendar setTimeZone: timeZone];
NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
return [weekdays objectAtIndex:theComponents.weekday];
}
#pragma mark 获取本周指定天的日期
- (NSString *)getDateInWeek:(int)day {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekday | NSCalendarUnitDay fromDate:self];
// 获取今天是星期几
NSInteger weekDay = [comp weekday];
// 获取今天是第几号
NSInteger currentDay = [comp day];;
// 计算当前日期和本周的星期一相差天数
long firstDiff = weekDay == 1 ? -6 : [calendar firstWeekday] - weekDay + 1;
// 在当前日期(去掉时分秒)基础上加上差的天数
NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:self];
[firstDayComp setDay:currentDay + firstDiff];
NSDate *firstDayOfWeek = [calendar dateFromComponents:firstDayComp];
if (day == 0 || day > 7) {
day = 7;
}
NSDate *correctDate = [NSDate dateWithTimeInterval:(day - 1) * 24 * 60 * 60 sinceDate:firstDayOfWeek];//后day天
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM-dd"];
NSString *dayString = [formatter stringFromDate:correctDate];
return dayString;
}
#pragma mark 获取当前日期前后N天的日期
- (NSDate *)getDateWithDay:(int)day {
return [NSDate dateWithTimeInterval:day * 24 * 60 * 60 sinceDate:self];
}
#pragma mark 获取当前日期前后N月的日期
- (NSDate *)getDateWithMonth:(int)month {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *componentsToAdd = [[NSDateComponents alloc] init];
[componentsToAdd setMonth:month];
NSDate *dateAfterMonth = [calendar dateByAddingComponents:componentsToAdd toDate:self options:0];
return dateAfterMonth;
}
iOS 关于日期的方法
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 上次给大家分享了《2017年最全的excel函数大全(5)——逻辑函数》,这次分享给大家日期和时间函数(上)。 D...