1、获取本地精准时间(UTC → 本机)
//获取准确日期和时间(UTC → 本地)
+ (NSString *)getNowDateAndTime
{
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:[NSDate date] options:0];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
format.timeZone = [NSTimeZone localTimeZone];
NSString *dateString = [format stringFromDate:newDate];
return dateString;
}
2、获取本地精准的日期
//获取精准的日期
+ (NSString *)getNowDate
{
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:[NSDate date] options:0];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = @"yyyy-MM-dd";
format.timeZone = [NSTimeZone localTimeZone];
NSString *dateString = [format stringFromDate:newDate];
return dateString;
}
3、获取本地精准的昨天的日期
//获取精准的昨天日期
+ (NSString *)getYesterdayDate
{
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow:-(24*60*60)];
NSDate *yesterdayDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:yesterday options:0];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = @"yyyy-MM-dd";
format.timeZone = [NSTimeZone localTimeZone];
NSString *dateString = [format stringFromDate:yesterdayDate];
return dateString;
}
4、获取据今天 n天 的日期(例:3 → 3天后的日期,-4 → 4天前的日期)
//获取n天前的日期
+(NSString *)getNDay:(NSInteger)n{
NSDate*nowDate = [NSDate date];
NSDate* theDate;
if(n!=0){
NSTimeInterval oneDay = 24*60*60*1; //1天的长度
theDate = [nowDate initWithTimeIntervalSinceNow: oneDay*n ];//initWithTimeIntervalSinceNow是从现在往前后推的秒数
}else{
theDate = nowDate;
}
NSDateFormatter *date_formatter = [[NSDateFormatter alloc] init];
[date_formatter setDateFormat:@"YYYYMMdd"];
NSString *the_date_str = [date_formatter stringFromDate:theDate];
return the_date_str;
}
5、获取当前日期是周几
//根据日期获取是周几 返回数据为 : 一、二、三
+ (NSString*)weekdayStringFromDate:(NSDate*)inputDate {
NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"日", @"一", @"二", @"三", @"四", @"五", @"六", nil];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
[calendar setTimeZone: timeZone];
NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
return [weekdays objectAtIndex:theComponents.weekday];
}
6、获取下一周的日期,保存为数组
//获取下一周日期
- (NSMutableArray*)getNextWeekArray
{
NSMutableArray* nextWeekDateMultArr = [NSMutableArray array];
[nextWeekDateMultArr removeAllObjects];
NSString* str = [self weekdayStringFromDate:[NSDate date]];
NSArray* weekArr = [NSArray arrayWithObjects: @"一", @"二", @"三", @"四", @"五", @"六", @"日", nil];
NSInteger nextWeekNum = [weekArr indexOfObject:str];
for (int i = 0; i < 7; i++) {
//获取下周周一的日期
NSString* nextWeekStr = [self getNDay:(7-nextWeekNum + i)];
NSString *year = [nextWeekStr substringToIndex:4];
NSString *month = [nextWeekStr substringWithRange:NSMakeRange(4, 2)];
NSString *day = [nextWeekStr substringWithRange:NSMakeRange(6, 2)];
NSString* dateStr = [NSString stringWithFormat:@"%@-%@-%@",year,month,day];
[nextWeekDateMultArr addObject:dateStr];
}
return nextWeekDateMultArr;
}