1.生日正则表达式
#pragma 正则匹配生日
+ (BOOL)checkBirthNumber:(NSString *) birthNumber
{
NSString *pattern = @"((19[\\d]{2})|(20[0-1][\\d]))((0[1-9])|(1[0-2]))((0[1-9])|([1-2]\\d)|(3[0-1]))";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:birthNumber];
return isMatch;
}
2.获取当前日期
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
NSString *DateTime = [formatter stringFromDate:date];
NSLog(@"%@============年-月-日 时:分:秒=====================",DateTime);
3.获取当前的时间戳
NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
long long int date = (long long int)time;
NSString *timeSp = [NSString stringWithFormat:@"%lld",date];
4.根据当前日期计算前n个月或后n个月的日期
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM"];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setMonth:1]; (下个月)
// [comps setMonth:-1];(上个月)
NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *mDate = [calender dateByAddingComponents:comps toDate:date options:0];
NSString *dateTime = [formatter stringFromDate:mDate];
NSLog(@"locationStr:%@",_dateTime);
5. 根据日期判断是星期几
@第一种写法:
NSDateComponents *_comps = [[NSDateComponents alloc] init];
[_comps setDay:23];
[_comps setMonth:3];
[_comps setYear:2014];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *_date = [gregorian dateFromComponents:_comps];
NSDateComponents *weekdayComponents =
[gregorian components:NSWeekdayCalendarUnit fromDate:_date];
int _weekday = [weekdayComponents weekday];
NSLog(@"_weekday::%d",_weekday);
@第二种写法:
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDate *now = [NSDate date];
// 话说在真机上需要设置区域,才能正确获取本地日期,天朝代码:zh_CN
calendar.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
comps = [calendar components:unitFlags fromDate:now];
6.计算2个日期相差天数
/**
* 计算2个日期相差天数
* startDate 起始日期
* endDate 截至日期
*/
-(NSInteger)daysFromDate:(NSDate *)startDate toDate:(NSDate *)endDate {
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
// 话说在真机上需要设置区域,才能正确获取本地日期,天朝代码:zh_CN
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//得到相差秒数
NSTimeInterval time = [endDate timeIntervalSinceDate:startDate];
int days = ((int)time)/(3600*24);
int hours = ((int)time)%(3600*24)/3600;
int minute = ((int)time)%(3600*24)/3600/60;
if (days <= 0 && hours <= 0&&minute<= 0) {
NSLog(@"0天0小时0分钟");
return 0;
}
else {
NSLog(@"%@",[[NSString alloc] initWithFormat:@"%i天%i小时%i分钟",days,hours,minute]);
// 之所以要 + 1,是因为 此处的days 计算的结果 不包含当天 和 最后一天\
(如星期一 和 星期四,计算机 算的结果就是2天(星期二和星期三),日常算,星期一——星期四相差3天,所以需要+1)\
对于时分 没有进行计算 可以忽略不计
return days + 1;
}
}
7.计算当前日期与指定日期做对比
第一步:先获取系统当前日期,即本节简书当中的第2个知识点
第二步:先查看指定日期的格式,如是:2018-01-01,即可直接比较,如是:2018-01-01-00,需转换为相同的日期格式,返回NSDate形式
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy-HHmmss"];
NSDate *date = [dateFormatter dateFromString:@"30-09-2016-000000"];
第三步:进行比较,如果没达到指定日期,返回-1,刚好是这一时间,返回0,否则返回1
- (int)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy-HHmmss"];
NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];
NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];
NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
NSComparisonResult result = [dateA compare:dateB];
NSLog(@"date1 : %@, date2 : %@", oneDay, anotherDay);
if (result == NSOrderedDescending) {
NSLog(@"Date1 is in the future");
return 1;
}
else if (result == NSOrderedAscending){
NSLog(@"没有达到指定日期");
return -1;
}
NSLog(@"两时间相同");
return 0;
}
第四步:比较调用第三步函数
[self compareOneDay:[self getCurrentTime] withAnotherDay:date]