/**
返回时间戳(单位:秒)
*/
+(long)dateCurrent;
/**
* 获取iPhone时间(数组形式)。
* @return @"2016" @"2" @"18" @"11" @"26" @"00"
*/
+(NSArray*)dateMembers;
/**
* 获取iPhone时间(字符串形式)。
* @return @"20160218112600"
*/
+ (NSString*)dateString;
/**
* 编辑日期时间
*/
+(NSDate*)makeDateYear:(int)y
Month:(int)mo
Day:(int)d
Hour:(int)h
Min:(int)m
Sec:(int)s;
/**
* 将字符串日期分解时间成员,@"20160218112600" => @"2016" @"2" @"18" @"11" @"26" @"00"
* @param time @"20160218112600"
* @return @"2016" @"2" @"18" @"11" @"26" @"00"
*/
+ (NSArray*)divideDateMembers:(NSString*)time;
/**
* 时间值加“秒”值。
* @param sec 秒数
* @param date @"20160218112600"
*/
+ (NSString*)addSec:(NSTimeInterval)sec OnDate:(NSString *)date;
/**
* 求两个时间点差值。(timeA-timeB)
* @param timeA @"20160218112601"
* @param timeB @"20160218112600"
*/
+(NSTimeInterval)differenceOfDateA:(NSString*)timeA AndDateB:(NSString*)timeB;
/**
* 求两个时间点差值。(dateA-dateB)
* @param dateA
* @param dateB
*/
+(NSTimeInterval)gapOfDateA:(NSDate*)dateA DateB:(NSDate*)dateB;
/**
* 当前【天】
*/
+ (NSInteger)day:(NSDate *)date;
/**
* 当前【月】
*/
+ (NSInteger)month:(NSDate *)date;
/**
* 当前【年】
*/
+ (NSInteger)year:(NSDate *)date;
/**
* 当前【星期】
*/
+ (NSInteger)weekday:(NSDate *)date;
/**
* 这周是这个月的第几周?
*/
+ (NSInteger)firstWeekdayInThisMonth:(NSDate *)date;
/**
* 获取当前月天数
*/
+ (NSInteger)totaldaysInMonth:(NSDate *)date;
/**
* 上/下m个月后的时间。
*/
+ (NSDate*)changeMonth:(int)monDate:(NSDate*)date;
/**
* 上/下d个天后的时间。
*/
+ (NSDate*)changeDay:(int)donDate:(NSDate*)date;
/**
* 秒数换算
* 输入 :60秒 (uint32_t)
* 返回 :00:01:00 (NSString*)
*/
+(NSString*)stringFromSec:(uint32_t)sec;
+(long)dateCurrent{
NSDate*datenow = [NSDatedate];
longtime = (long)([datenowtimeIntervalSince1970]);
returntime;
}
+(NSArray*)dateMembers
{
NSDate*date = [NSDatedate];// 获得时间对象
NSDateFormatter *forMatter = [[NSDateFormatter alloc] init];
[forMattersetDateFormat:@"yyyy-MM-dd-HH-mm-ss"];
NSString*dateStr = [forMatterstringFromDate:date];
NSArray * arr = [dateStr componentsSeparatedByString:@"-"];
returnarr;
}
+(NSDate*)makeDateYear:(int)yMonth:(int)moDay:(int)dHour:(int)hMin:(int)mSec:(int)s{
NSString*dateStr = [NSStringstringWithFormat:@"%d-%d-%d-%d-%d-%d",y,mo,d,h,m,s];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormattersetDateFormat:@"yyyy-MM-dd-HH-mm-ss"];
NSDate*destDate= [dateFormatterdateFromString:dateStr];
returndestDate;
}
+ (NSArray*)divideDateMembers:(NSString*)time{
if(time.length!=14)returnnil;
NSString * y = [time substringWithRange:NSMakeRange(0, 4)];
NSString * mo= [time substringWithRange:NSMakeRange(4, 2)];
NSString * d = [time substringWithRange:NSMakeRange(6, 2)];
NSString * h = [time substringWithRange:NSMakeRange(8, 2)];
NSString * m = [time substringWithRange:NSMakeRange(10, 2)];
NSString * s = [time substringWithRange:NSMakeRange(12, 2)];
NSArray* array =@[y,mo,d,h,m,s];
returnarray;
}
+ (NSString*)dateString{
NSDateFormatter *format = [NSDateFormatter new];
[formatsetDateFormat: @"yyyyMMddHHmmss"];
NSString* string = [formatstringFromDate:[NSDatedate]];
returnstring;
}
+ (NSString*)addSec:(NSTimeInterval)sec OnDate:(NSString *)date{
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormattersetDateFormat:@"yyyyMMddHHmmss"];
/*--- 原来时间值 ---*/
NSDate*destDate= [dateFormatterdateFromString:date];
/*--- 在原来时间值+sec秒值 ---*/
NSDate*addSecDate = [destDatedateByAddingTimeInterval:sec];
NSString*addSecDateString = [dateFormatterstringFromDate:addSecDate];
returnaddSecDateString;
}
/*
* 用途:求两个时间点差值。
*/
+(NSTimeInterval)differenceOfDateA:(NSString*)timeA AndDateB:(NSString*)timeB{
NSDateFormatter * format = [NSDateFormatter new];
[formatsetDateFormat: @"yyyyMMddHHmmss"];
NSDate* dateA = [formatdateFromString:timeA];
//NSTimeInterval late1=[dateA timeIntervalSince1970]*1;
NSDate* dateB = [formatdateFromString:timeB];
//NSTimeInterval late2=[dateB timeIntervalSince1970]*1;
//long ret = (long)(late2 - late1);
NSTimeIntervalgap = [dateAtimeIntervalSinceDate:dateB];
returngap;
}
+(NSTimeInterval)gapOfDateA:(NSDate*)dateA DateB:(NSDate*)dateB{
NSTimeIntervalgap = [dateAtimeIntervalSinceDate:dateB];
returngap;
}
+ (NSInteger)day:(NSDate *)date{
NSCalendar*calendar = [NSCalendarcurrentCalendar];
[calendarsetTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
return[componentsday];
}
+ (NSInteger)month:(NSDate *)date{
NSCalendar*calendar = [NSCalendarcurrentCalendar];
[calendarsetTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
return[componentsmonth];
}
+ (NSInteger)year:(NSDate *)date{
NSCalendar*calendar = [NSCalendarcurrentCalendar];
[calendarsetTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
return[componentsyear];
}
+ (NSInteger)weekday:(NSDate *)date{
NSCalendar*calendar = [NSCalendarcurrentCalendar];
[calendarsetTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay|NSCalendarUnitWeekday) fromDate:date];
return[componentsweekday];
}
+ (NSInteger)firstWeekdayInThisMonth:(NSDate *)date{
NSCalendar*calendar = [NSCalendarcurrentCalendar];
[calendarsetFirstWeekday:1];//1.Sun. 2.Mon. 3.Thes. 4.Wed. 5.Thur. 6.Fri. 7.Sat.
NSDateComponents *comp = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
[compsetDay:1];
NSDate*firstDayOfMonthDate = [calendardateFromComponents:comp];
NSUInteger firstWeekday = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth forDate:firstDayOfMonthDate];
returnfirstWeekday -1;
}
+ (NSInteger)totaldaysInMonth:(NSDate *)date{
NSRange daysInLastMonth = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
returndaysInLastMonth.length;
}
+ (NSDate*)changeMonth:(int)monDate:(NSDate*)date{
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.month= m;
NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
returnnewDate;
}
+ (NSDate*)changeDay:(int)donDate:(NSDate*)date{
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.day= d;
NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
returnnewDate;
}
+(NSString*)stringFromSec:(uint32_t)sec{
uint32_tm_sec = sec%60;
uint32_tm_min = ((sec-m_sec)/60)%60;
uint32_tm_hour= (sec-m_min*60-m_sec)/3600;
NSString*s_sec = [selftextSec:m_sec];
NSString*s_min = [selftextSec:m_min];
NSString*s_hour= [selftextSec:m_hour];
if(m_hour <=0) {
return[NSStringstringWithFormat:@"%@:%@",s_min,s_sec];
}else{
return[NSStringstringWithFormat:@"%@:%@:%@",s_hour,s_min,s_sec];
}
}
+(NSString*)textSec:(uint32_t)sec{
if(sec<10) {
return[NSStringstringWithFormat:@"0%d",sec];
}else{
return[NSStringstringWithFormat:@"%d",sec];
}
}