1.//得到当前时间
- (NSString *)getCurrentTime {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString *dateTime = [formatter stringFromDate:[NSDate date]];
return dateTime;
}
//将字符串转成NSDate类型
- (NSDate *)dateFromString:(NSString *)dateString {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: @"yyyy-MM-dd"];
NSDate *destDate= [dateFormatter dateFromString:dateString];
return destDate;
}
NSDate *date = [NSDate date];//当前时间
NSDate *lastDay = [NSDate dateWithTimeInterval:-24*60*60 sinceDate:date];//前一天
NSDate *nextDat = [NSDate dateWithTimeInterval:24*60*60 sinceDate:date];//后一天
2.//得到当前月份或者(年份)的上一个 和 下一个月份(年份)
-(void)setUPAndDownDate:(NSInteger)num{
NSDate *currentDate = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM"];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *lastMonthComps = [[NSDateComponents alloc] init];
//[lastMonthComps setYear:1];
[lastMonthComps setMonth:num];
NSDate *newdate = [calendar dateByAddingComponents:lastMonthComps toDate:currentDate options:0];
NSString *dateStr = [formatter stringFromDate:newdate];
NSLog(@"date str = %@", dateStr);
}
//日期
-(NSString *)getCurrntTimeDay:(NSInteger)num Date:(NSDate *)date{
NSDate *nextDat = [NSDate dateWithTimeInterval:num*24*60*60 sinceDate:date];//后num天
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM月dd日"];
NSString *dateTime = [formatter stringFromDate:nextDat];
return dateTime;
}
//返回星期几
-(NSString*)weekdayStringFromDate:(NSDate*)inputDate {
NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
return [weekdays objectAtIndex:theComponents.weekday];
}
//直接调用 正数表示往后的月份 ,负数表示往前的月份。
多多支持!!!