ios 时间一系列的问题
获取今天字符串
+(NSString *)getCurrentDay{
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd"];
NSString *currentDateStr = [dateFormatter stringFromDate:currentDate];
return currentDateStr;
}
获取昨天字符串
+(NSString *)getYesdayDay{
NSDate *currentDate = [NSDate date];
currentDate = [NSDate dateWithTimeInterval:-24*60*60 sinceDate:currentDate];//前一天
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd"];
NSString *currentDateStr = [dateFormatter stringFromDate:currentDate];
return currentDateStr;
}
时间戳转NSDate
-(NSDate *)nsstringConversionNSDate:(NSString *)dateStr
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSDate *datestr = [dateFormatter dateFromString:dateStr];
return datestr;
}
NSDate转字符串日期
- (NSString *)dateConversionTimeStamp:(NSDate *)date{
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
return timeSp;
}
时间戳转字符串日期
-(NSString *)timeStampConversionNSString:(NSString *)timeStamp{
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timeStamp longLongValue]];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateStr = [formatter stringFromDate:date];
return dateStr;
}
四,NSDate日期 获取 星期几
- (NSString *) getweekDayStringWithDate:(NSDate *) date{
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; // 指定日历的算法
NSDateComponents *comps = [calendar components:NSWeekdayCalendarUnit fromDate:date];// 1 是周日,2是周一 3.以此类推
NSNumber * weekNumber = @([comps weekday]);NSInteger weekInt = [weekNumber integerValue];
NSString *weekDayString = @"周一";
switch (weekInt) {
case 1:{weekDayString = @"周日";}
break;
case 2: {
weekDayString = @"周一";}
break;
case 3:{
weekDayString = @"周二";
}
break;
case 4:{
weekDayString = @"周三";
}
break;
case 5:{
weekDayString = @"周四";
}
break;case 6:{
weekDayString = @"周五";
}
break;
case 7:{
weekDayString = @"周六";
}
break;
default:
break;}
return weekDayString;
}
当前时间所在一周第一天
+ (NSString *)getCurrentWeekFirstDay{
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday
fromDate:now];
// 得到星期几
// 1(星期天) 2(星期二) 3(星期三) 4(星期四) 5(星期五) 6(星期六) 7(星期天)
NSInteger weekDay = [comp weekday];
// 得到几号
NSInteger day = [comp day];
NSLog(@"weekDay:%ld day:%ld",weekDay,day);
// 计算当前日期和这周的星期一和星期天差的天数
long firstDiff,lastDiff;
if (weekDay == 1) {
firstDiff = 1;
lastDiff = 0;
}else{
firstDiff = ([calendar firstWeekday] - weekDay)+1;
lastDiff = 7 - weekDay;
}
NSArray *currentWeeks = [self getCurrentWeeksWithFirstDiff:firstDiff lastDiff:lastDiff];
// 在当前日期(去掉了时分秒)基础上加上差的天数
NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];
[firstDayComp setDay:day + firstDiff];
NSDate *firstDayOfWeek= [calendar dateFromComponents:firstDayComp];
NSDateComponents *lastDayComp = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];
[lastDayComp setDay:day + lastDiff];
NSDate *lastDayOfWeek= [calendar dateFromComponents:lastDayComp];
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"yyyy-MM-dd"];
NSString *currentDateStr = [formater stringFromDate:firstDayOfWeek];
return currentDateStr;
}
获取一周时间 数组
+ (NSMutableArray *)getCurrentWeeksWithFirstDiff:(NSInteger)first lastDiff:(NSInteger)last{
NSMutableArray *eightArr = [[NSMutableArray alloc] init];
for (NSInteger i = first; i < last + 1; i ++) {
//从现在开始的24小时
NSTimeInterval secondsPerDay = i * 24*60*60;
NSDate *curDate = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"M月d日"];
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];
//组合时间
NSString *strTime = [NSString stringWithFormat:@"%@(%@)",dateStr,weekStr]; [eightArr addObject:strTime];
}
return eightArr;
}