DateUtility.h
// 日期类的方法集合
#define DAY_OF_SECONDS 24*60*60
#define DEFAULT_DATE_TEMPLATE_STRING @"yyyyMMddHHmmss"
//#define DEFAULT_DATE_TEMPLATE_STRING @"HH:mm:ss dd/MM/yy"
#define SHORT_DATE_TEMPLATE_STRING @"yyyy-MM-dd"
#define HOUR_MINUT_TEMPLATE_STRING @"HH:mm"
struct MNSDate {
NSUInteger second;
NSUInteger minute;
NSUInteger hour;
NSUInteger weekday;
NSUInteger day;
NSUInteger month;
NSUInteger year;
};
typedef struct MNSDate MNSDate;
/**
* 描述:将日期转化为MNSDate对象
* 参数:要转化的日期
* 返回值:MNSDate值,可以跟对象属性获取相应的年,月,日,时,分,秒
*
*/
CG_INLINE MNSDate MNSDateMake(NSDate *date) {
MNSDate MNSDate;
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSSecondCalendarUnit | NSMinuteCalendarUnit | NSHourCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:date];
MNSDate.second = [components second];
MNSDate.minute = [components minute];
MNSDate.hour = [components hour];
MNSDate.weekday = [components weekday];
MNSDate.day = [components day];
MNSDate.month = [components month];
MNSDate.year = [components year];
return MNSDate;
}
/**
* 描述:获取从fromdate开始,经过天后的日期
* 参数:fromDate:开始日期 dayGap:天数
* 返回值:计算后的日期
*
*/
CG_INLINE NSDate * getDate(NSDate *fromDate, NSInteger dayGap) {
return [NSDate dateWithTimeInterval:DAY_OF_SECONDS * dayGap sinceDate:fromDate];
}
/**
* 描述:获取当前的本地日期
* 参数:时间
* 返回值:本地日期
*
*/
CG_INLINE NSDate * getLocalDateFromGMTDate(NSDate *GMTDate) {
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: GMTDate];
NSDate *localeDate = [GMTDate dateByAddingTimeInterval: interval];
return localeDate;
}
/**
* 描述:获取当前的月最后一天的日期
* 参数:月份
* 返回值:当前的月最后一天的日期
*
*/
CG_INLINE NSDate * getLastDateOfMonth(NSDate *targetMonthDay) {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSRange daysRange = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:targetMonthDay];
NSDateComponents *components = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:targetMonthDay];
[components setDay:daysRange.length];
return [calendar dateFromComponents:components];
}
/**
* 描述:获取当前的月第一天的日期
* 参数:月份
* 返回值:当前的月第一天的日期
*
*/
CG_INLINE NSDate * getFirstDateOfMonth(NSDate *targetMonthDay) {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:targetMonthDay];
[components setDay:1];
return [calendar dateFromComponents:components];
}
/**
* 描述:获取一周第一天的日期
* 参数:日期
* 返回值:一周第一天的日期
*
*/
CG_INLINE NSDate *getFirstDateOfWeek(NSDate *date) {
MNSDate MNSDate = MNSDateMake(date);
return getDate(date, -1 * (MNSDate.weekday - 1));
}
/**
* 描述:获取明天日期
* 参数:日期
* 返回值:明天的日期
*
*/
CG_INLINE NSDate *getTomorrowDate() {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *today = [NSDate date];
NSDateComponents *components = [calendar components:kCFCalendarUnitHour | kCFCalendarUnitMinute | kCFCalendarUnitSecond fromDate:today];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
NSInteger second = [components second];
NSTimeInterval timeInterval = DAY_OF_SECONDS - 3600 * hour - 60 * minute - second + 1;
return [NSDate dateWithTimeInterval:timeInterval sinceDate:today];;
}
/**
* 描述: 将日期字符串按格式转化为nsdate对象
* 参数:dateStringTemplate:日期格式(如:YY-MM-DD) dateStr:日期的字符串表达
* 返回值:转化后的nsdate对象
*
*/
CG_INLINE NSDate * getDateFromString(NSString *dateStringTemplate, NSString *dateStr) {
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
if (!dateStringTemplate) {
dateStringTemplate = DEFAULT_DATE_TEMPLATE_STRING;
}
[dateFormatter setDateFormat:dateStringTemplate];
NSDate *date = [dateFormatter dateFromString:dateStr];
return date;
}
/**
* 描述: 将nsdate对象按格式转化为字符串
* 参数:dateStringTemplate:日期格式(如:YY-MM-DD) date:nsdate对象
* 返回值:转化后的字符串对象
*
*/
CG_INLINE NSString *getStringFromDate(NSString *dateStringTemplate, NSDate *date) {
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
if (!dateStringTemplate) {
dateStringTemplate = DEFAULT_DATE_TEMPLATE_STRING;
}
[dateFormatter setDateFormat:dateStringTemplate];
NSString *dateStr = [dateFormatter stringFromDate:date];
return dateStr;
}