iOS 时间戳和时间互换,计算两日期相隔天数
/*
*获取当前系统时间的时间戳
*/
+(NSInteger)getNowTimestamp;
/**
* 获取当前时间
*/
+ (NSString *)getNowTimeTampFormat:(NSString *)tampFormat;
/*
*时间戳转时间
*format (@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
*/
+(NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format;
/*
*时间戳转时间(时间的前一天)
*format (@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
*/
+(NSString *)timestampSwitchlastTime:(NSInteger)timestamp andFormatter:(NSString *)format;
/*
*时间戳转时间
*formatTime 时间字符串
*format (@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
*/
+ (NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format;
/*
*某日和当前日期相隔多少天
*/
+ (NSInteger)getDifferenceByDate:(NSString *)date;
/**
*日期格式请传入:2013-08-05 12:12:12;如果修改日期格式,比如:2013-08-05,则将[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];修改为[df setDateFormat:@"yyyy-MM-dd"];
*/
+(int)compareDate:(NSString*)date01 withDate:(NSString*)date02;
+(NSInteger)getNowTimestamp{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
//设置时区,这个对于时间的处理有时很重要
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
NSDate *datenow = [NSDate date];//现在时间
// NSLog(@"设备当前的时间:%@",[formatter stringFromDate:datenow]);
//时间转时间戳的方法:
NSInteger timeSp = [[NSNumber numberWithDouble:[datenow timeIntervalSince1970]] integerValue];
// NSLog(@"设备当前的时间戳:%ld",(long)timeSp); //时间戳的值
return timeSp;
}
+ (NSString *)getNowTimeTampFormat:(NSString *)tampFormat {
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:tampFormat];
NSString *DateTime = [formatter stringFromDate:date];
return DateTime;
}
//将某个时间戳转化成 时间
#pragma mark - 将某个时间戳转化成 时间
+(NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:format];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
//十三位时间戳转时间需要/1000 十位时间戳你不需要
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp / 1000];
NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
// NSLog(@"&&&&&&&confromTimespStr = : %@",confromTimespStr);
return confromTimespStr;
}
/*
*时间戳转时间(时间的前一天)
*format (@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
*/
+(NSString *)timestampSwitchlastTime:(NSInteger)timestamp andFormatter:(NSString *)format {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:format];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
//十三位时间戳转时间需要/1000 十位时间戳你不需要
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp / 1000];
NSDate *lastDay = [NSDate dateWithTimeInterval:(-24*60*60)*2 sinceDate:confromTimesp];//前两天
NSString *confromTimespStr = [formatter stringFromDate:lastDay];
return confromTimespStr;
}
//将某个时间转化成 时间戳
#pragma mark - 将某个时间转化成 时间戳
+ (NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:format];
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
NSDate* date = [formatter dateFromString:formatTime]; //------------将字符串按formatter转成nsdate
//时间转时间戳的方法:
NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
// NSLog(@"将某个时间转化成 时间戳&&&&&&&timeSp:%ld",(long)timeSp); //时间戳的值
return timeSp;
}
+ (NSInteger)getDifferenceByDate:(NSString *)date {
//获得当前时间
// NSDate *now = [NSDate date];
// //实例化一个NSDateFormatter对象
// NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// //设定时间格式
// [dateFormatter setDateFormat:@"YYYY-MM-dd"];
// NSDate *oldDate = [dateFormatter dateFromString:date];
// NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
// unsigned int unitFlags = NSDayCalendarUnit;
// NSDateComponents *comps = [gregorian components:unitFlags fromDate:oldDate toDate:now options:0];
// return [comps day];
//
//下面这种方法从00:00:00开始计算
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setFirstWeekday:2];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd"];
NSDate *fromDate;
NSDate *toDate;
[gregorian rangeOfUnit:NSDayCalendarUnit startDate:&fromDate interval:NULL forDate:[dateFormatter dateFromString:date]];
[gregorian rangeOfUnit:NSDayCalendarUnit startDate:&toDate interval:NULL forDate:[NSDate date]];
NSDateComponents *dayComponents = [gregorian components:NSDayCalendarUnit fromDate:fromDate toDate:toDate options:0];
#pragma clang diagnostic pop
return [dayComponents day];
}
+(int)compareDate:(NSString*)date01 withDate:(NSString*)date02 {
int ci;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd"];
NSDate *dt1 = [[NSDate alloc] init];
NSDate *dt2 = [[NSDate alloc] init];
dt1 = [df dateFromString:date01];
dt2 = [df dateFromString:date02];
NSComparisonResult result = [dt1 compare:dt2];
switch (result) {
//date02比date01大
case NSOrderedAscending: ci=1; break;
//date02比date01小
case NSOrderedDescending: ci=-1; break;
//date02=date01
case NSOrderedSame: ci=0; break;
default: NSLog(@"erorr dates %@, %@", dt2, dt1); break;
}
return ci;
}