- .h文件
#import <Foundation/Foundation.h>
@interface TimeTool : NSObject
+(NSDateComponents *)getNowTime; //获取当前的时间
+(NSString *)dateOfTodayNoYear; //当天的日期(不包含年份)
///当天的日期 %d年%d月%d日
+(NSString *)dateOfToday;
///当天的日期 %d-%d-%d
+(NSString *)dateOfTodaySimple;
+(NSString *)dateOfThisWeek; //本周的日期
///获取本周的日期 数组
+(NSArray *)dateOfThisWeekByNowDay;
+(NSString *)dateOfThisMonth; //本月的日期
///获取本月的日期
+(NSArray *)dateOfThisMonthByNowday;
///生成的记录号 年月日时分秒 yyyyMMddHHmmss
+(NSString *)timeForRecord;
///生成的记录号 年月日时分秒 yyyy-MM-dd HH:mm:ss
+(NSString *)timeForRecordSecond;
///生成记录号 年月日
+(NSString *)timeForRecordYMD;
+(NSString *)year;
+(NSString *)month;
+(NSString *)day;
+(NSInteger )week;
+(NSInteger )hour;
+(NSInteger )minute;
+(NSInteger )second;
+(NSString *)hourAndMinute;
+(NSString *)hourAndMinuteAndSecond;
+(NSString *)yearMonthDayHourMinuteSecond;
-
.m 文件
+(NSDateComponents *)getNowTime //获取当前的时间
{NSCalendar *calendar = [[NSCalendar alloc] //获取当前的时间initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *now = [NSDate date] ; NSDateComponents *comps1 = [[NSDateComponents alloc] init] ; NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; comps1 = [calendar components:unitFlags fromDate:now]; return comps1;
}
+(NSString *)dateOfToday //当天的日期
{
NSDateComponents *comps = [GetTimeAll getNowTime];
NSInteger year = [comps year];
NSInteger month = [comps month];
NSInteger day = [comps day];
NSString *TimeOfToday = [NSString stringWithFormat:@"%d年%d月%d日",(int)year,(int)month,(int)day];
return TimeOfToday;
}
+(NSString *)dateOfTodaySimple
{
NSDateComponents *comps = [GetTimeAll getNowTime];
NSInteger year = [comps year];
NSInteger month = [comps month];
NSInteger day = [comps day];
NSString *TimeOfToday = [NSString stringWithFormat:@"%d-%d-%d",(int)year,(int)month,(int)day];
return TimeOfToday;
}
+(NSString *)dateOfTodayNoYear //当天的日期
{
NSDateComponents *comps = [GetTimeAll getNowTime];
// NSInteger week = [comps weekday];
// NSInteger year = [comps year];
NSInteger month = [comps month];
NSInteger day = [comps day];
// NSInteger hour = [comps hour];
// NSInteger min = [comps minute];
// NSInteger sec = [comps second];
// NSLog(@"%d年%d月%d日,星期%d",year,month,day,week);
// NSLog(@"%d点%d分%d秒",hour,min,sec);
NSString *TimeOfToday = [NSString stringWithFormat:@"%d月%d日",(int)month,(int)day];
return TimeOfToday;
}
+(NSString *)dateOfThisWeek //本周的日期
{
NSDateComponents *comps = [GetTimeAll getNowTime];
NSInteger week = [comps weekday];
//获取当前的时间
NSDate *now = [NSDate date] ;
//周一的日期
NSDate *mondayDate = [now dateByAddingTimeInterval:-(week-2) *3600 *24 ];
NSDateFormatter *mondayFor = [[NSDateFormatter alloc] init];
[mondayFor setDateFormat:@"MM月,dd日"];
NSString *mondayStr = [mondayFor stringFromDate:mondayDate];
//周日的日期
NSDate *weekDayDate = [now dateByAddingTimeInterval:(7-week +1) *3600 *24 ];
NSDateFormatter *weekDayFor = [[NSDateFormatter alloc] init];
[weekDayFor setDateFormat:@"MM月,dd日"];
NSString *weekDayStr = [weekDayFor stringFromDate:weekDayDate];
NSString *weekDateStr = [NSString stringWithFormat:@"%@--%@",mondayStr,weekDayStr];
// NSLog(@"weekDateStr = %@",weekDateStr);
return weekDateStr;
}
///获取本周的日期 数组
+(NSArray *)dateOfThisWeekByNowDay
{
NSDateComponents *comps = [GetTimeAll getNowTime];
NSInteger week = [comps weekday];
//获取当前的时间
NSDate *now = [NSDate date] ;
if (week > 1 && week <= 7) {
week = week - 1;
}else
{
week = 7;
}
NSDateFormatter *mondayFor = [[NSDateFormatter alloc] init];
[mondayFor setDateFormat:@"MM-dd"];
//周1的日期
NSDate *weekDate1 = [now dateByAddingTimeInterval:(7-week -6) *3600 *24 ];
NSString *weekStr1 = [mondayFor stringFromDate:weekDate1];
//周2的日期
NSDate *weekDate2 = [now dateByAddingTimeInterval:(7-week -5) *3600 *24 ];
NSString *weekStr2 = [mondayFor stringFromDate:weekDate2];
//周2的日期
NSDate *weekDate3 = [now dateByAddingTimeInterval:(7-week -4) *3600 *24 ];
NSString *weekStr3 = [mondayFor stringFromDate:weekDate3];
//周2的日期
NSDate *weekDate4 = [now dateByAddingTimeInterval:(7-week -3) *3600 *24 ];
NSString *weekStr4 = [mondayFor stringFromDate:weekDate4];
//周2的日期
NSDate *weekDate5 = [now dateByAddingTimeInterval:(7-week -2) *3600 *24 ];
NSString *weekStr5 = [mondayFor stringFromDate:weekDate5];
//周2的日期
NSDate *weekDate6 = [now dateByAddingTimeInterval:(7-week -1) *3600 *24 ];
NSString *weekStr6 = [mondayFor stringFromDate:weekDate6];
//周2的日期
NSDate *weekDate7 = [now dateByAddingTimeInterval:(7-week) *3600 *24 ];
NSString *weekStr7 = [mondayFor stringFromDate:weekDate7];
NSMutableArray *WeekArr = [NSMutableArray arrayWithObjects:weekStr1,weekStr2,weekStr3,weekStr4,weekStr5,weekStr6,weekStr7, nil];
[WeekArr replaceObjectAtIndex:week - 1 withObject:@"今天"];
return [NSArray arrayWithArray:WeekArr];
}
+(NSString *)dateOfThisMonth //本月的日期
{
NSDate *now = [NSDate date] ; //获取当前的时间
NSDateComponents *comps = [GetTimeAll getNowTime];
NSInteger month = [comps month];
NSInteger day = [comps day];
NSInteger year = [comps year];
//1号的日期
NSDate *onedayDate = [now dateByAddingTimeInterval:-(day-1) *3600 *24 ];
NSDateFormatter *onedayFor = [[NSDateFormatter alloc] init];
[onedayFor setDateFormat:@"MM月,dd日"];
NSString *onedayStr = [onedayFor stringFromDate:onedayDate];
//30号的日期
if (month == 1||month == 3 || month == 5 ||month == 7 || month == 8 || month == 10 || month == 12) {
NSDate *lastdayDate = [now dateByAddingTimeInterval:(31 - day) *3600 *24 ];
NSDateFormatter *lastdayFor = [[NSDateFormatter alloc] init];
[lastdayFor setDateFormat:@"MM月,dd日"];
NSString *lastdayStr = [lastdayFor stringFromDate:lastdayDate];
NSString *monDateStr = [NSString stringWithFormat:@"%@--%@",onedayStr,lastdayStr];
return monDateStr;
}else if(month == 2)
{
if ((year%4 == 0&&year%100 !=0) || year%400 ==0) { //闰年
NSDate *lastdayDate = [now dateByAddingTimeInterval:(29 - day) *3600 *24 ];
NSDateFormatter *lastdayFor = [[NSDateFormatter alloc] init];
[lastdayFor setDateFormat:@"MM月,dd日"];
NSString *lastdayStr = [lastdayFor stringFromDate:lastdayDate];
NSString *monDateStr = [NSString stringWithFormat:@"%@--%@",onedayStr,lastdayStr];
return monDateStr;
}else
{
NSDate *lastdayDate = [now dateByAddingTimeInterval:(28 - day) *3600 *24 ];
NSDateFormatter *lastdayFor = [[NSDateFormatter alloc] init];
[lastdayFor setDateFormat:@"MM月,dd日"];
NSString *lastdayStr = [lastdayFor stringFromDate:lastdayDate];
NSString *monDateStr = [NSString stringWithFormat:@"%@--%@",onedayStr,lastdayStr];
return monDateStr;
}
}else
{
NSDate *lastdayDate = [now dateByAddingTimeInterval:(30 - day) *3600 *24 ];
NSDateFormatter *lastdayFor = [[NSDateFormatter alloc] init];
[lastdayFor setDateFormat:@"MM月,dd日"];
NSString *lastdayStr = [lastdayFor stringFromDate:lastdayDate];
NSString *monDateStr = [NSString stringWithFormat:@"%@--%@",onedayStr,lastdayStr];
return monDateStr;
}
}
///获取本月的日期
+(NSArray *)dateOfThisMonthByNowday
{
//获取当前的时间
NSDate *now = [NSDate date] ;
NSDateComponents *comps = [GetTimeAll getNowTime];
NSInteger month = [comps month];
NSInteger day = [comps day];
NSInteger year = [comps year];
NSDateFormatter *onedayFor = [[NSDateFormatter alloc] init];
[onedayFor setDateFormat:@"dd"];
NSMutableArray *dateArr = [[NSMutableArray alloc] init];
//30号的日期
if (month == 1||month == 3 || month == 5 ||month == 7 || month == 8 || month == 10 || month == 12) {
for (int i = 30; i >= 0; i--) {
NSDate *lastdayDate = [now dateByAddingTimeInterval:(31 - day - i) *3600 *24 ];
NSString *dayStr = [onedayFor stringFromDate:lastdayDate];
[dateArr addObject:dayStr];
}
[dateArr replaceObjectAtIndex:day - 1 withObject:@"今\n天"];
}else if(month == 2)
{
if ((year%4 == 0&&year%100 !=0) || year%400 ==0) { //闰年
for (int i = 28; i >=0 ; i--) {
NSDate *lastdayDate = [now dateByAddingTimeInterval:(29 - day - i) *3600 *24 ];
NSString *lastdayStr = [onedayFor stringFromDate:lastdayDate];
[dateArr addObject:lastdayStr];
}
[dateArr replaceObjectAtIndex:day - 1 withObject:@"今\n天"];
}else
{
for (int i = 27; i >= 0; i--) {
NSDate *lastdayDate = [now dateByAddingTimeInterval:(28 - day - i) *3600 *24 ];
NSString *lastdayStr = [onedayFor stringFromDate:lastdayDate];
[dateArr addObject:lastdayStr];
}
[dateArr replaceObjectAtIndex:day - 1 withObject:@"今\n天"];
}
}else
{
for (int i = 29; i >=0 ; i--) {
NSDate *lastdayDate = [now dateByAddingTimeInterval:(30 - day - i) *3600 *24 ];
NSString *lastdayStr = [onedayFor stringFromDate:lastdayDate];
[dateArr addObject:lastdayStr];
}
[dateArr replaceObjectAtIndex:day - 1 withObject:@"今天"];
}
return [NSArray arrayWithArray:dateArr];;
}
+(NSString *)timeForRecord
{
NSString *timeStr = nil;
NSDate *now = [NSDate date] ;
NSDateFormatter *onedayFor = [[NSDateFormatter alloc] init];
[onedayFor setDateFormat:@"yyyyMMddHHmmss"];
timeStr = [onedayFor stringFromDate:now];
return timeStr;
}
+(NSString *)timeForRecordSecond
{
NSString *timeStr = nil;
NSDate *now = [NSDate date] ;
NSDateFormatter *onedayFor = [[NSDateFormatter alloc] init];
[onedayFor setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
timeStr = [onedayFor stringFromDate:now];
return timeStr;
}
+(NSString *)timeForRecordYMD
{
NSString *timeStr = nil;
NSDate *now = [NSDate date] ;
NSDateFormatter *onedayFor = [[NSDateFormatter alloc] init];
[onedayFor setDateFormat:@"yyMMdd"];
timeStr = [onedayFor stringFromDate:now];
return timeStr;
}
+(NSString *)year;
{
NSString *timeStr = nil;
NSDate *now = [NSDate date] ;
NSDateFormatter *onedayFor = [[NSDateFormatter alloc] init];
[onedayFor setDateFormat:@"yyyy"];
timeStr = [onedayFor stringFromDate:now];
return timeStr;
}
+(NSString *)month
{
NSString *timeStr = nil;
NSDate *now = [NSDate date] ;
NSDateFormatter *onedayFor = [[NSDateFormatter alloc] init];
[onedayFor setDateFormat:@"MM"];
timeStr = [onedayFor stringFromDate:now];
return timeStr;
}
+(NSString *)day
{
NSString *timeStr = nil;
NSDate *now = [NSDate date] ;
NSDateFormatter *onedayFor = [[NSDateFormatter alloc] init];
[onedayFor setDateFormat:@"dd"];
timeStr = [onedayFor stringFromDate:now];
return timeStr;
}
+(NSInteger )week
{
NSDateComponents *comps = [GetTimeAll getNowTime];
NSInteger week = [comps week];
return week;
}
+(NSInteger )hour
{
NSDateComponents *comps = [GetTimeAll getNowTime];
NSInteger hour = [comps hour];
return hour;
}
+(NSInteger )minute
{
NSDateComponents *comps = [GetTimeAll getNowTime];
NSInteger minute = [comps minute];
return minute;
}
+(NSInteger )second
{
NSDateComponents *comps = [GetTimeAll getNowTime];
NSInteger second = [comps second];
return second;
}
+(NSString *)hourAndMinute
{
NSDateComponents *comps = [GetTimeAll getNowTime];
NSInteger hour = [comps hour];
NSDateComponents *comps1 = [GetTimeAll getNowTime];
NSInteger minute = [comps1 minute];
NSString *hourAndMinute = [NSString stringWithFormat:@"%d点%d分",(int)hour,(int)minute];
return hourAndMinute;
}
+(NSString *)hourAndMinuteAndSecond
{
NSDateComponents *comps = [GetTimeAll getNowTime];
NSInteger hour = [comps hour];
NSDateComponents *comps1 = [GetTimeAll getNowTime];
NSInteger minute = [comps1 minute];
NSDateComponents *comps2 = [GetTimeAll getNowTime];
NSInteger second = [comps2 second];
NSString *hourAndMinuteAndSecond = [NSString stringWithFormat:@"%d:%d:%d",(int)hour,(int)minute,(int)second];
return hourAndMinuteAndSecond;
}
+(NSString *)yearMonthDayHourMinuteSecond
{
NSDateComponents *comps1 = [GetTimeAll getNowTime];
NSInteger year = [comps1 year];
NSDateComponents *comps2 = [GetTimeAll getNowTime];
NSInteger month = [comps2 month];
NSDateComponents *comps3 = [GetTimeAll getNowTime];
NSInteger day = [comps3 day];
NSDateComponents *comps4 = [GetTimeAll getNowTime];
NSInteger hour = [comps4 hour];
NSDateComponents *comps5 = [GetTimeAll getNowTime];
NSInteger minute = [comps5 minute];
NSDateComponents *comps6 = [GetTimeAll getNowTime];
NSInteger second = [comps6 second];
NSString *str = [NSString stringWithFormat:@"%d%d%d%d%d%d",(int)year,(int)month,(int)day,(int)hour,(int)minute,(int)second];
return str;
}