开发过程中,我们开会经常用用到获取当前时间、明天的常用的方法。下面是我总接的获取时间相关的常用的方法,当然了为了方便大家使用,我写成了类方法(+方法),希望对大家有所帮助。
/**
获取当地当前时间
@return 时间字符串
*/
+ (NSString *)getCurrentTime {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// yyyy-MM-dd 可自定义,也可以换成 yyyy-MM-dd HH:MM:SS
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString *dateTime = [formatter stringFromDate:[NSDate date]];
return dateTime;
}
/**
将字符串转成NSDate类型
@param dateString 时间类型的字符串 例如:2018-07-12
@return date
*/
+ (NSDate *)dateFromString:(NSString *)dateString {
NSDateFormatter *inputFormatter= [[NSDateFormatter alloc] init];
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[inputFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *inputDate = [inputFormatter dateFromString:dateString];
//inputDate或出现相差八小时问题,下面是解决相差八小时
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: inputDate];
NSDate *localeDate = [inputDate dateByAddingTimeInterval: interval];
return localeDate;
}
/**
传入今天的时间,返回明天的时间
@param aDate 当前的时间NSDate类型,上面的方法可获取当前时间的NSDate类型
@return 明天时间
*/
+ (NSString *)getTomorrowDay:(NSDate *)aDate {
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [gregorian components:NSCalendarUnitWeekday |NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:aDate];
[components setDay:([components day]+1)];
NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
NSDateFormatter *dateday = [[NSDateFormatter alloc] init];
[dateday setDateFormat:@"yyyy-MM-dd"];
return [dateday stringFromDate:beginningOfWeek];
}
/**
判断是否为今天
@param todayTime 时间(NSDate)
@return YES,是 NO ~
*/
+ (BOOL)isTodayWithTime:(NSDate*)todayTime{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:[NSDate date]];
NSDate *today = [cal dateFromComponents:components];
components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:todayTime];
NSDate *otherDate = [cal dateFromComponents:components];
if([today isEqualToDate:otherDate]){
return YES;
}
return NO;
}
/**
获取当前时间戳
*1000 是精确到毫秒,不乘就是精确到秒
@return 返回时间戳
*/
+ (NSString *)currentTimeStr{
NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];//获取当前时间0秒后的时间
NSTimeInterval time=[date timeIntervalSince1970]*1000;// *1000 是精确到毫秒,不乘就是精确到秒
NSString *timeString = [NSString stringWithFormat:@"%.0f", time];
return timeString;
}
/**
时间戳转时间,时间戳为13位是精确到毫秒的,10位精确到秒
传入的时间戳str如果是精确到毫秒的记得要/1000
@param timeStr 时间戳
@return 返回时间
*/
+ (NSString *)getDateStringWithTimeStr:(NSString *)timeStr{
NSTimeInterval time=[timeStr doubleValue]/1000;//传入的时间戳str如果是精确到毫秒的记得要/1000
NSDate *detailDate=[NSDate dateWithTimeIntervalSince1970:time];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; //实例化一个NSDateFormatter对象
//设定时间格式,这里可以设置成自己需要的格式
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss SS"];
NSString *currentDateStr = [dateFormatter stringFromDate: detailDate];
return currentDateStr;
}
/**
字符串转时间戳 如:2017-4-10 17:15:10
@param time 时间
@return 时间戳
*/
+ (NSString *)getTimeStrWithString:(NSString *)time{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];// 创建一个时间格式化对象
[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; //设定时间的格式
NSDate *tempDate = [dateFormatter dateFromString:time];//将字符串转换为时间对象
NSString *timeStr = [NSString stringWithFormat:@"%ld", (long)[tempDate timeIntervalSince1970]*1000];//字符串转成时间戳,精确到毫秒*1000
return timeStr;
}
/**
获取当前是星期几
@return 1、2、3、4、5、6、7(星期六是 7 ,星期日 1)
*/
+ (NSInteger)getNowWeekday{
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday |
NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDate *now = [NSDate date];
// 在真机上需要设置区域,才能正确获取本地日期,中国区代码:zh_CN
calendar.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
comps = [calendar components:unitFlags fromDate:now];
return [comps weekday];
}
/**
获取当前是几月几日
@return 07月07日
*/
+ (NSString *)getTodayStr{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:[NSDate date]];
NSDate *today = [cal dateFromComponents:components];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"MM月dd日";
return [formatter stringFromDate:today];
}
/**
计算两个时间的差值
@param starTime 开始时间
@param endTime 结束时间
@return 时间差 (单位 :秒)
*/
+ (NSTimeInterval)pleaseInsertStarTime:(NSString *)starTime andInsertEndTime:(NSString *)endTime{
NSDateFormatter* formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"yyyy-MM-dd"];//根据自己的需求定义格式
NSDate* startDate = [formater dateFromString:starTime];
NSDate* endDate = [formater dateFromString:endTime];
NSTimeInterval time = [endDate timeIntervalSinceDate:startDate];
return time;
}
一共大约是十个时间相关的方法,每一个方法都有详细的注释,希望对大家有所帮助