小工具的功能:
获取系统当前时间
将时间转为十位时间戳
将时间转换为十三位时间戳
将时间戳转换为时间
获取两个时间的时间间隔,起止时间传入格式为:yyyy-MM-dd hh:mm:ss, 结果返回:10位时间戳
获取两个时间的时间间隔,传入起始与结束时间的十位时间戳,返回的时间差值格式为:hh:mm:ss
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
//说明:这里处理的时间都为当地时间、时间格式Formatter可以传nil,默认值为@"yyyy-MM-dd hh:mm:ss"
//注意:YYYY为时间所在周对应的年份,如果某一天所在的周是跨年的,那么得到的结果可能小一年,因此传入yyyy
@interface DataTime : NSObject
//获取系统当前时间
+(void)getTheCurrentSystemTimeWithFormatter:(NSString *)formatterStr withBlock:(void(^)(NSString* time, BOOL success, NSString * error))block;
//将时间转为十位时间戳
+(void)getTenBitTimestampWithTime:(NSString *)time formatter:(NSString *)formatterStr withBlock:(void(^)(NSString* time, BOOL success, NSString * error))block;
//将时间转换为十三位时间戳
+(void)getThirteenBitTimestampWithTime:(NSString *)time formatter:(NSString *)formatterStr withBlock:(void(^)(NSString* time, BOOL success, NSString * error))block;
//将时间戳转换为时间
+(void)getTimeFromTenBitTimesWithBitTime:(NSString *)bitTime formatter:(NSString *)formatterStr withBlock:(void(^)(NSString* time, BOOL success, NSString * error))block;
//获取两个时间的时间间隔,起止时间传入格式为:yyyy-MM-dd hh:mm:ss, 结果返回:10位时间戳
+(void)getBitTimeDifferenceValueWithStartTime:(NSString *)startTime endTime:(NSString *)endTime withBlock:(void(^)(NSString* time, BOOL success, NSString * error))block;
//获取两个时间的时间间隔,传入起始与结束时间的十位时间戳,返回的时间差值格式为:hh:mm:ss
+(void)getTimeDifferenceValueWithStartTime:(NSString *)startTime endTime:(NSString *)endTime withBlock:(void(^)(NSString* time, BOOL success, NSString * error))block;
@end
NS_ASSUME_NONNULL_END
#import "DataTime.h"
@implementation DataTime
+(void)getTheCurrentSystemTimeWithFormatter:(NSString *)formatterStr withBlock:(void(^)(NSString* time, BOOL success, NSString * error))block
{
if (formatterStr == nil || formatterStr.length == 0) {
formatterStr = [NSString stringWithFormat:@"yyyy-MM-dd hh:mm:ss"];
}
NSDateFormatter * formatter = [[NSDateFormatter alloc ] init];
[formatter setDateFormat:formatterStr];
NSString* date = [formatter stringFromDate:[NSDate date]];
NSString * timeNow = [[NSString alloc] initWithFormat:@"%@", date];
if (timeNow.length > 0) {
block(timeNow, YES, nil);
}
else{
block(nil, NO, @"获取本地时间失败");
}
}
+(void)getTenBitTimestampWithTime:(NSString *)time formatter:(NSString *)formatterStr withBlock:(void(^)(NSString* time, BOOL success, NSString * error))block
{
if (!time) {
block(nil, NO, @"error:初始时间为空");
return;
}
else{
if (time.length == 0) {
block(nil, NO, @"error:初始时间为空");
return;
}else{}
}
if (!formatterStr) {
formatterStr = [NSString stringWithFormat:@"yyyy-MM-dd hh:mm:ss"];
}
else{
if (formatterStr.length == 0) {
formatterStr = [NSString stringWithFormat:@"yyyy-MM-dd hh:mm:ss"];
}else{}
}
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:formatterStr];
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[formatter setTimeZone:timeZone];
NSDate *datenow = [formatter dateFromString:time];
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
block(timeSp, YES, nil);
}
+(void)getThirteenBitTimestampWithTime:(NSString *)time formatter:(NSString *)formatterStr withBlock:(void(^)(NSString* time, BOOL success, NSString * error))block
{
if (!time) {
block(nil, NO, @"error:初始时间为空");
return;
}
else{
if (time.length == 0) {
block(nil, NO, @"error:初始时间为空");
return;
}else{}
}
if (!formatterStr) {
formatterStr = [NSString stringWithFormat:@"yyyy-MM-dd hh:mm:ss"];
}
else{
if (formatterStr.length == 0) {
formatterStr = [NSString stringWithFormat:@"yyyy-MM-dd hh:mm:ss"];
}else{}
}
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:formatterStr];
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[formatter setTimeZone:timeZone];
NSDate *datenow = [formatter dateFromString:time];
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970] * 1000];
block(timeSp, YES, nil);
}
+(void)getTimeFromTenBitTimesWithBitTime:(NSString *)bitTime formatter:(NSString *)formatterStr withBlock:(void(^)(NSString* time, BOOL success, NSString * error))block
{
if (!bitTime) {
block(nil, NO, @"error:时间戳数据为空");
return;
}
else{
if (bitTime.length == 0) {
block(nil, NO, @"error:时间戳数据为空");
return;
}else{}
}
if (!formatterStr) {
formatterStr = [NSString stringWithFormat:@"yyyy-MM-dd hh:mm:ss"];
}
else{
if (formatterStr.length == 0) {
formatterStr = [NSString stringWithFormat:@"yyyy-MM-dd hh:mm:ss"];
}else{}
}
NSTimeInterval interval=[bitTime doubleValue];
if (bitTime.length > 10) {
interval = interval / 1000.0;
}
NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:formatterStr];
NSString * time = [formatter stringFromDate:date];
if (time.length > 0) {
block(time, YES, nil);
}
else{
block(nil, NO, @"时间转换出错");
}
}
+(void)getBitTimeDifferenceValueWithStartTime:(NSString *)startTime endTime:(NSString *)endTime withBlock:(void(^)(NSString* time, BOOL success, NSString * error))block
{
if ([startTime isEqualToString:endTime]) {
block(@"0", YES, @"起始与结束时间相等");
return;
}
NSDateFormatter * formatter = [[NSDateFormatter alloc ] init];
[formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[formatter setTimeZone:timeZone];
NSDate * st_date = [formatter dateFromString:startTime];
NSInteger st_time = (long)[st_date timeIntervalSince1970];
NSDate * et_date = [formatter dateFromString:endTime];
NSInteger et_time = (long)[et_date timeIntervalSince1970];
NSInteger dif_time = et_time - st_time;
NSString * time = [NSString stringWithFormat:@"%ld", dif_time];
if (time.length > 0) {
block(time, YES, nil);
}
else{
block(nil, NO, @"时间差值计算异常");
}
}
+(void)getTimeDifferenceValueWithStartTime:(NSString *)startTime endTime:(NSString *)endTime withBlock:(void(^)(NSString* time, BOOL success, NSString * error))block
{
if (startTime == endTime) {
block(@"0", YES, @"起始与结束时间相等");
return;
}
NSTimeInterval st_interval=[startTime doubleValue];
if (startTime.length > 10) {
st_interval = st_interval / 1000.0;
}
NSTimeInterval et_interval=[endTime doubleValue];
if (endTime.length > 10) {
et_interval = et_interval / 1000.0;
}
NSDate *et_date = [NSDate dateWithTimeIntervalSince1970:et_interval];
NSDate *st_date = [NSDate dateWithTimeIntervalSince1970:st_interval];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ;//只比较天数差异
//比较的结果是NSDateComponents类对象
NSDateComponents *delta = [calendar components:unit fromDate:st_date toDate:et_date options:0];
NSInteger year = delta.year;
NSInteger month = delta.month;
NSInteger day = delta.day;
NSInteger hour = delta.hour;
NSInteger min = delta.minute;
NSInteger sec = delta.second;
NSMutableString * time = [[NSMutableString alloc] initWithFormat:@""];
if (year > 0) {
[time appendFormat:@"%ld年", year];
}else{}
if (month > 0) {
[time appendFormat:@"%ld月", month];
}else{}
if (day > 0) {
[time appendFormat:@"%ld天 ", day];
}else{}
if (hour > 0) {
if (hour > 9) {
[time appendFormat:@"%ld:", hour];
}
else{
[time appendFormat:@"0%ld:", hour];
}
}else{}
if (min > 0) {
if (min > 9) {
[time appendFormat:@"%ld:", min];
}
else{
[time appendFormat:@"0%ld:", min];
}
}else{}
if (sec > 0) {
if (sec > 9) {
[time appendFormat:@"%ld", sec];
}
else{
[time appendFormat:@"0%ld", sec];
}
}else{}
if (time.length > 0) {
block(time, YES, nil);
}
else{
block(time, NO, @"时间差值计算异常");
}
}
@end