1、获取当前时间
//获取当前时间
- (NSString *)currentDateStr{
NSDate *currentDate = [NSDate date];//获取当前时间,日期
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];// 创建一个时间格式化对象
[dateFormatter setDateFormat:@"YYYY/MM/dd hh:mm:ss SS "];//设定时间格式,这里可以设置成自己需要的格式
NSString *dateString = [dateFormatter stringFromDate:currentDate];//将时间转化成字符串
return dateString;
}
2、获取当前时间戳
pragma mark 当前时间戳
+ (NSString *)nowTimeInterval {
// 现在的时间戳
// 获取当前时间0秒后的时间
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0];
// *1000 是精确到毫秒,不乘就是精确到秒
NSTimeInterval time = [date timeIntervalSince1970]*1000;
NSString *timeStr = [NSString stringWithFormat:@"%.0f", time];
return timeStr;
}
3、时间戳 转日期
#pragma mark 时间戳 转日期 (yyyy-MM-dd)
+ (NSString *)timeWithYearMonthDayCountDown:(NSString *)timestamp {
// 时间戳转日期
// 传入的时间戳timeStr如果是精确到毫秒的记得要/1000
NSTimeInterval timeInterval = [timestamp doubleValue]/1000;
NSDate *detailDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// 实例化一个NSDateFormatter对象,设定时间格式,这里可以设置成自己需要的格式
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *dateStr = [dateFormatter stringFromDate:detailDate];
return dateStr;
}
4、字符串转时间戳
// 字符串转时间戳 如:2017-4-10 17:15:10 (精确到毫秒*1000)
+ (NSString *)getTimeStrWithString:(NSString *)str {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];// 创建一个时间格式化对象
[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; //设定时间的格式
NSDate *tempDate = [dateFormatter dateFromString:str];//将字符串转换为时间对象
NSString *timeStr = [NSString stringWithFormat:@"%ld", (long)[tempDate timeIntervalSince1970]*1000];//字符串转成时间戳,精确到毫秒*1000
return timeStr;
}
5、时间戳 转换成 (天 时 分 秒)
#pragma mark 转换成 (天 时 分 秒)
+ (NSString *)timeFormatted:(NSInteger)totalSeconds {
// NSInteger seconds = totalSeconds % 60;
// NSInteger minutes = (totalSeconds / 60) % 60;
// NSInteger hours = totalSeconds / 3600 % 24;
// NSInteger day = totalSeconds / 3600 / 24;
NSInteger day = (totalSeconds/(3600*24));
NSInteger hour = ((totalSeconds-day*24*3600)/3600);
NSInteger minute = (totalSeconds-day*24*3600-hour*3600)/60;
NSInteger second = totalSeconds - day*24*3600 - hour*3600 - minute*60;
return [NSString stringWithFormat:@"%ld天%ld时%ld分%ld秒", day, hour, minute, second];
}
6、时间戳转日期
#pragma mark 时间戳 转日期 (yyyy-MM-dd HH:mm:ss SS)
// 时间戳转时间,时间戳为13位是精确到毫秒的,10位精确到秒
+ (NSString *)dateWithTimeInterval:(NSString *)timeStr {
// 传入的时间戳timeStr如果是精确到秒的记得要/1000
NSTimeInterval timeInterval = [timeStr doubleValue]/1000;
NSDate *detailDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// 实例化一个NSDateFormatter对象,设定时间格式,这里可以设置成自己需要的格式
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss SS"];
NSString *dateStr = [dateFormatter stringFromDate:detailDate];
return dateStr;
}
7、字符串转时间戳
// 字符串转时间戳 如:2017-4-10 17:15:10
- (NSString *)getTimeStrWithString:(NSString *)str{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];// 创建一个时间格式化对象
[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; //设定时间的格式
NSDate *tempDate = [dateFormatter dateFromString:str];//将字符串转换为时间对象
NSString *timeStr = [NSString stringWithFormat:@"%ld", (long)[tempDate timeIntervalSince1970]*1000];//字符串转成时间戳,精确到毫秒*1000
return timeStr;
}
8、获取某一天是星期几
// 1、获取某一天是星期几
+(NSString *)getWeekDayFordate:(NSTimeInterval)data{
NSArray *weekday = [NSArray arrayWithObjects:[NSNull null], @"7",@"1", @"2", @"3", @"4", @"5", @"6", nil];
NSDate *newDate = [NSDate dateWithTimeIntervalSince1970:data];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar components:NSCalendarUnitWeekday fromDate:newDate];
NSString *weekStr = [weekday objectAtIndex:components.weekday];
return weekStr;
}
#pragma mark 1、获取某一天是星期几
+(NSString *)getWeekDayForDateTimeStamp:(NSTimeInterval)timeStamp{
NSArray *weekday = [NSArray arrayWithObjects:[NSNull null], @"周日",@"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
NSDate *newDate = [NSDate dateWithTimeIntervalSince1970:timeStamp];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar components:NSCalendarUnitWeekday fromDate:newDate];
NSString *weekStr = [weekday objectAtIndex:components.weekday];
return weekStr;
}
9、获取当月的天数
+(NSInteger)getNumberOfDaysInCurrentMonth
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:[NSDate date]];
NSUInteger numberOfDaysInMonth = range.length;
return numberOfDaysInMonth;
}
10、刚刚、几分钟之前、几小时之前、几天、几月、几年之前
+ (NSString *)jk_time1TurnStringTimestamp:(NSString *)timestamp
{
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timestamp doubleValue]];
NSTimeInterval timeInterval = [date timeIntervalSinceNow];
NSInteger seconds = round(timeInterval);
if (seconds < 0) {
return @"";
}
if (seconds < 60) {
return @"刚刚";
}else if (seconds < 3600) {
return [NSString stringWithFormat:@"%ld分钟之前",seconds/60];
}else if (seconds < 3600*24){
return [NSString stringWithFormat:@"%ld小时之前",seconds/(3600)];
}else if (seconds < (3600*24*30)){
return [NSString stringWithFormat:@"%ld天之前",seconds/(3600*24)];
}else if (seconds < (3600*24*30*12)){
return [NSString stringWithFormat:@"%ld月之前",seconds/(3600*24*30)];
}else{
return [NSString stringWithFormat:@"%ld年之前",seconds/(3600*24*30*12)];
}
}
11、两个日期NSDate之间相隔多少天(月、年)
1.根据两个日期(NSDate),算出它们相差多少天。
解决方法
NSCalendar类提供了计算差值的接口,可以计算两个日期相差多少年月日时分秒等
代码
//创建两个日期
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *startDate = [dateFormatter dateFromString:@"2017-07-26"];
NSDate *endDate = [dateFormatter dateFromString:@"2017-09-01"];
//利用NSCalendar比较日期的差异
NSCalendar *calendar = [NSCalendar currentCalendar];
/**
* 要比较的时间单位,常用如下,可以同时传:
* kCFCalendarUnitYear : 年
* kCFCalendarUnitMonth : 月
* NSCalendarUnitDay : 天
* kCFCalendarUnitHour : 小时
* kCFCalendarUnitMinute : 分钟
* kCFCalendarUnitSecond : 秒
*/
NSCalendarUnit unit = NSCalendarUnitDay;//只比较天数差异
//比较的结果是NSDateComponents类对象
NSDateComponents *delta = [calendar components:unit fromDate:startDate toDate:endDate options:0];
//打印
NSLog(@"%@",delta);
//获取其中的"天"
NSLog(@"%ld",delta.day);
打印结果
![](http://img.e-com-net.com/image/info10/e827baf9e1594a07ab6a9eb28e0c87fc.jpg)
image
注意事项
1、如果同时比较月、天的时候,结果是N个月零N天,不会再是37天,例如
NSCalendarUnit unit1 = NSCalendarUnitDay | NSCalendarUnitMonth;//同时比较天数、月份差异
//比较的结果是NSDateComponents类对象
NSDateComponents *delta1 = [calendar components:unit1 fromDate:startDate toDate:endDate options:0];
//打印
NSLog(@"%@",delta1);
打印结果
![](http://img.e-com-net.com/image/info10/d0168af5bf014ce69f5a966f6873f156.jpg)
image
即1个月零6天。
2、在遇到2月,闰年等不标准时期,这种方式的运算结果非常准确。
12、返回当前时间的: 年、月、日、小时、分钟、秒
#pragma mark -- 返回当前时间的: 年、月、日、小时、分钟、秒 --
+ (NSDictionary *)dateCalendarReturnYear_Month_Day_Hour_Minute_Second {
/** iOS 获取当前的年、月、日、小时、分钟、秒 */
// 获取当前的date
NSDate *nowDate = [NSDate date];
// 使用NSCalendar 计算详细的时间
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = kCFCalendarUnitYear | //年
kCFCalendarUnitMonth | //月份
NSCalendarUnitDay | //日
kCFCalendarUnitHour | //小时
kCFCalendarUnitMinute | //分钟
kCFCalendarUnitSecond; // 秒
NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:nowDate];
NSInteger year = [dateComponent year];
NSInteger month = [dateComponent month];
NSInteger day = [dateComponent day];
NSInteger hour = [dateComponent hour];
NSInteger minute = [dateComponent minute];
NSInteger second = [dateComponent second];
NSDictionary *dateDic = @{
@"year":[NSString stringWithFormat:@"%ld", year],
@"month":[NSString stringWithFormat:@"%ld", month],
@"day":[NSString stringWithFormat:@"%ld", day],
@"hour":[NSString stringWithFormat:@"%ld", hour],
@"minute":[NSString stringWithFormat:@"%ld", minute],
@"second":[NSString stringWithFormat:@"%ld", second]
};
return dateDic;
}
13、获取当前时间~未来一周的数据, 数组包含 周几、日期几月几号:[NSDate dateWithTimeIntervalSinceNow:0]; // 如果是0 的话,就是当前时间
/** 数组包含 周几、日期几号 */
- (NSArray *)getWeekDateArray {
// 获取当前时间开始,未来一周的时间数组,只要日期:几日
NSMutableArray *weekArr = [NSMutableArray arrayWithCapacity:0];
for (int i = 0; i < 7; i++) {
//从现在开始的24小时
NSTimeInterval currentTimeInterval = i * 24*60*60;
NSDate *curDate = [NSDate dateWithTimeIntervalSinceNow:currentTimeInterval]; // 如果是0 的话,就是当前时间
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd"];
NSString *dateStr = [dateFormatter stringFromDate:curDate];//几月几号
//NSString *dateStr = @"5月31日";
NSDateFormatter *weekFormatter = [[NSDateFormatter alloc] init];
[weekFormatter setDateFormat:@"EEEE"];//星期几 @"HH:mm 'on' EEEE MMMM d"];
NSString *weekStr = [weekFormatter stringFromDate:curDate];
//转换文案
weekStr = [self transWeekName:weekStr];
//组合时间
NSString *strTime = [NSString stringWithFormat:@"%@-%@",dateStr,weekStr];
NSLog(@"%@", strTime);
NSDictionary *dateDic = @{
@"week":weekStr,
@"day":dateStr
};
[weekArr addObject:dateDic];
}
return [weekArr copy];
}
- (NSString *)transWeekName:(NSString *)orrignWeekName {
NSString *targetWeekName = @"";
//转换文案
if ([orrignWeekName isEqualToString:@"Monday"]) {
targetWeekName = @"周一";
}
else if ([orrignWeekName isEqualToString:@"Tuesday"]) {
targetWeekName = @"周二";
}
else if ([orrignWeekName isEqualToString:@"Wednesday"]) {
targetWeekName = @"周三";
}
else if ([orrignWeekName isEqualToString:@"Thursday"]) {
targetWeekName = @"周四";
}
else if ([orrignWeekName isEqualToString:@"Friday"]) {
targetWeekName = @"周五";
}
else if([orrignWeekName isEqualToString:@"Saturday"]) {
targetWeekName = @"周六";
} else { // Sunday
targetWeekName = @"周日";
}
return targetWeekName;
}