iOS NSDate 扩展

NSDate+CF.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSInteger, NSDateFormaterMode) {
    NSDateFormaterModeDefault,      /** 年-月-日 时:分:秒 */
    NSDateFormaterModeTime,         /** 年-月-日 时:分 */
    NSDateFormaterModeDay,          /** 年-月-日 */
    NSDateFormaterModeDayAnother,   /** *年*月*日 */
    NSDateFormaterModeTimeSecond,   /** 时:分:秒 */
    NSDateFormaterModeTimeMinute    /** 时:分 */
};

@interface NSDateFormatter (CF)

+ (NSDateFormatter *)dateFormaterWithMode:(NSDateFormaterMode)formaterMode;

@end

@interface NSDate (CF)

@property (nonatomic, assign) NSInteger year;               //年
@property (nonatomic, assign) NSInteger month;              //月
@property (nonatomic, assign) NSInteger day;                //日
@property (nonatomic, assign) NSInteger hour;               //时
@property (nonatomic, assign) NSInteger minute;             //分
@property (nonatomic, assign) NSInteger second;             //秒

@property (nonatomic, assign) NSInteger weekday;            //周几
@property (nonatomic, assign) NSInteger weekmonth;          //第几周(本月)
@property (nonatomic, assign) NSInteger weekyear;           //第几周(本年)
@property (nonatomic, copy)   NSString *weekStr;            //星期几

@property (nonatomic, copy)   NSString *yesterday;          //昨天
@property (nonatomic, copy)   NSString *tomorrow;           //明天
@property (nonatomic, copy)   NSString *dayAfterTomorrow;   //后天

//date -> 格式化date
+(NSDate *)dateToDate:(NSDate *)date formaterMode:(NSDateFormaterMode)formaterMode;

//字符串 -> date
+(NSDate *)stringToDate:(NSString *)string formaterMode:(NSDateFormaterMode)formaterMode;

//data -> 字符串   获取指定格式的日期字符串
+(NSString *)dateToString:(NSDate *)date formaterMode:(NSDateFormaterMode)formaterMode;

//字符串 -> 字符串   获取指定格式的日期字符串
+(NSString *)regularDateString:(NSString *)originalDate formaterMode:(NSDateFormaterMode)formaterMode;

//判断是否为今天
+ (BOOL)isTodayWithDate:(NSDate *)date;

//判断是否为昨天
+ (BOOL)isYesterdayWithDate:(NSDate *)date;

//判断是否为昨天、今天、明天、后天……
+ (NSInteger)dayWithDate:(NSDate *)date;

/*
 * 需要传入的时间格式 2017-06-14 14:18:54    暂不考虑跨年
 * 时间显示规则
 * 当天       时:分
 * 前一天      昨天
 * 其余       月:日
 */
+(NSString *)inputTimeStr:(NSString *)timeStr;

//计算时长 天 - 小时 - 分
+(NSString *)intervalSinceFromDate:(NSString *)fromDate toDate:(NSString *)toDate dateModel:(NSDateFormaterMode)dateModel;


//时间戳转日期
+(NSString *)timestampToDate:(NSString *)timestamp;

//日期对比
+ (int)compareOneDate:(NSDate *)date1 withAnotherDate:(NSDate *)date2;

//日期对比
+ (int)compareOneDateString:(NSString *)date1 withAnotherDateString:(NSString *)date2;

//获取农历日期
+(NSDictionary *)getChineseCalendarWithDate:(NSDate *)date;

+ (NSInteger)minuteStartTime:(NSString *)startTime endTime:(NSString *)endTime;

//计算时间差
+ (NSString *)dateInterval:(NSDate *)date time:(NSInteger)minute;

//跳转到偏差时间点
+(NSDate *)getResultTime:(NSString *)time second:(NSInteger)second;

//获取当前时间戳  (以秒为单位)
+(NSString *)getNowTimeTimestamp;

//获取当前时间戳  (以毫秒为单位)
+(NSString *)getNowTimeTimestamp3;

//将本地日期字符串转为UTC日期字符串
//本地日期格式:2013-08-03 12:53:51
//可自行指定输入输出格式
+(NSString *)getUTCFormateLocalDate:(NSString *)localDate;

//将UTC日期字符串转为本地时间字符串
//输入的UTC日期格式2013-08-03T04:53:51+0000
+(NSString *)getLocalDateFormateUTCDate:(NSString *)utcDate;

@end

NS_ASSUME_NONNULL_END

NSDate+CF.m

#import "NSDate+CF.h"

@implementation NSDateFormatter (CF)

+ (NSDateFormatter *)dateFormaterWithMode:(NSDateFormaterMode)formaterMode {
    NSString *stringFormat = @"";
    
    switch (formaterMode) {
            
        case NSDateFormaterModeDefault:
            stringFormat = @"yyyy-MM-dd HH:mm:ss";
            break;
            
        case NSDateFormaterModeTime:
            stringFormat = @"yyyy-MM-dd HH:mm";
            break;
        
        case NSDateFormaterModeDay:
            stringFormat = @"yyyy-MM-dd";
            break;
        
        case NSDateFormaterModeDayAnother:
            stringFormat = @"yyyy年MM月dd日";
            break;
            
        case NSDateFormaterModeTimeSecond:
            stringFormat = @"HH:mm:ss";
            break;
            
        case NSDateFormaterModeTimeMinute:
            stringFormat = @"HH:mm";
            break;
            
        default:
            stringFormat = @"yyyy-MM-dd HH:mm:ss";
            break;
    }
    
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:stringFormat];
    return dateFormat;
}

@end

@implementation NSDate (CF)

#pragma mark  - 年
-(NSInteger)year{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear fromDate:self];
    return [components year];
}

#pragma mark - 月
-(NSInteger)month{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitMonth fromDate:self];
    return [components month];
}

#pragma mark - 日
-(NSInteger)day{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitDay fromDate:self];
    return [components day];
}

#pragma mark - 时
-(NSInteger)hour{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitHour fromDate:self];
    return [components hour];
}

#pragma mark - 分
-(NSInteger)minute{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitMinute fromDate:self];
    return [components minute];
}

#pragma mark - 秒
-(NSInteger)second{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitSecond fromDate:self];
    return [components second];
}

-(NSString *)yesterday{
    return [self getResultDay:-1];
}

-(NSString *)tomorrow{
    return [self getResultDay:1];
}

-(NSString *)dayAfterTomorrow{
    return [self getResultDay:2];
}

-(NSString *)getResultDay:(NSInteger)com{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    int unit = NSCalendarUnitDay | NSCalendarUnitMonth |  NSCalendarUnitYear;
    
    // 1.获得当前时间的年月日
    NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
    
    [nowCmps setDay:([nowCmps day] + com)];
    
    NSDate *resultDate = [calendar dateFromComponents:nowCmps];
    
    return [NSDate dateToString:resultDate formaterMode:NSDateFormaterModeDay];
}

+(NSDate *)getResultTime:(NSString *)time second:(NSInteger)second {
    
    NSDate *date = [NSDate stringToDate:time formaterMode:NSDateFormaterModeDefault];
    NSDate *newDate = [NSDate dateWithTimeInterval:second sinceDate:date];
    return newDate;
}

#pragma mark - 根据date获取 - 周几
-(NSInteger)weekday{
    //起始值 1-周日, 2-周一, 3-周二, 4-周三, 5-周四, 6-周五, 7-周六
    //改变值 7-周日, 1-周一, 2-周二, 3-周三, 4-周四, 5-周五, 6-周六
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitWeekday fromDate:self];
    
    NSInteger week = components.weekday;
    
    switch (week) {
        case 1:
            week = 7; //星期日;
            break;
        case 2:
            week = 1; //星期一;
            break;
        case 3:
            week = 2; //星期二;
            break;
        case 4:
            week = 3; //星期三;
            break;
        case 5:
            week = 4; //星期四;
            break;
        case 6:
            week = 5; //星期五;
            break;
        case 7:
            week = 6; //星期六;
            break;
        default:
            week = 0; //星期;
            break;
    }
    
    return week;
}

#pragma mark - 周几 : 星期几
-(NSString *)weekStr{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitWeekday fromDate:self];
    
    NSInteger week = components.weekday;
    
    NSString *weekStr = @"";
    
    switch (week) {
        case 1:
            weekStr = @"星期日";
            break;
        case 2:
            weekStr = @"星期一";
            break;
        case 3:
            weekStr = @"星期二";
            break;
        case 4:
            weekStr = @"星期三";
            break;
        case 5:
            weekStr = @"星期四";
            break;
        case 6:
            weekStr = @"星期五";
            break;
        case 7:
            weekStr = @"星期六";
            break;
        default:
            weekStr = @"星期";
            break;
    }
    
    return weekStr;
}

#pragma mark - 根据date获取 - 本月第几周
-(NSInteger)weekmonth{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitWeekOfMonth fromDate:self];
    return components.weekOfMonth;
}

#pragma mark - 根据date获取 - 本年第几周
-(NSInteger)weekyear{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitWeekOfYear fromDate:self];
    return components.weekOfYear;
}

#pragma mark - 字符串 -> date
+(NSDate *)stringToDate:(NSString *)string formaterMode:(NSDateFormaterMode)formaterMode{
    NSDateFormatter *formater = [NSDateFormatter dateFormaterWithMode:formaterMode];
    return [formater dateFromString:string];
}

#pragma mark - data -> 字符串
+(NSString *)dateToString:(NSDate *)date formaterMode:(NSDateFormaterMode)formaterMode{
    NSDateFormatter *formater = [NSDateFormatter dateFormaterWithMode:formaterMode];
    return [formater stringFromDate:date];
}

#pragma mark - 字符串 -> 字符串   获取指定格式的日期字符串
+(NSString *)regularDateString:(NSString *)originalDate formaterMode:(NSDateFormaterMode)formaterMode{
    
    NSDateFormatter *formater = [NSDateFormatter dateFormaterWithMode:formaterMode];
    
    //将日期设置为标准格式
    NSDateFormatter *originalFormater = [NSDateFormatter dateFormaterWithMode:NSDateFormaterModeDefault];
    NSDate *date = [originalFormater dateFromString:originalDate];
    //获取指定格式的日期字符串
    NSString *newDate = [formater stringFromDate:date];
    return newDate;
}

#pragma mark - date -> 格式化date
+(NSDate *)dateToDate:(NSDate *)date formaterMode:(NSDateFormaterMode)formaterMode{
    
    NSDateFormatter *formater = [NSDateFormatter dateFormaterWithMode:formaterMode];
    NSString *selfStr = [formater stringFromDate:date];
    return [formater dateFromString:selfStr];
}

#pragma mark - 判断是否为今天
+ (BOOL)isTodayWithDate:(NSDate *)date{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    int unit = NSCalendarUnitDay | NSCalendarUnitMonth |  NSCalendarUnitYear;
    
    // 1.获得当前时间的年月日
    NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
    
    // 2.获得self的年月日
    NSDateComponents *selfCmps = [calendar components:unit fromDate:date];
    
    return (selfCmps.year == nowCmps.year) && (selfCmps.month == nowCmps.month) && (selfCmps.day == nowCmps.day);
}

#pragma mark - 判断是否为昨天
+ (BOOL)isYesterdayWithDate:(NSDate *)date{
    
    /*
     * 将日期格式化为 年-月-日 类型
     * 计算格式化后的时间差是否为一天
     */
    
    //获取格式化当前日期
    NSDate *nowDate = [self dateToDate:[NSDate date] formaterMode:NSDateFormaterModeDay];

    //获取格式化目标日期
    NSDate *selfDate = [self dateToDate:date formaterMode:NSDateFormaterModeDay];

    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    // 获得nowDate和selfDate的差距
    NSDateComponents *cmps = [calendar components:NSCalendarUnitDay fromDate:selfDate toDate:nowDate options:0];
    
    return cmps.day == 1;
}

#pragma mark - 判断是否为昨天、今天、明天、后天……
+ (NSInteger)dayWithDate:(NSDate *)date{
    
    /*
     * 将日期格式化为 年-月-日 类型
     * 计算格式化后的时间差是否为一天
     */
    
    //获取格式化当前日期
    NSDate *nowDate = [self dateToDate:[NSDate date] formaterMode:NSDateFormaterModeDay];
    
    //获取格式化目标日期
    NSDate *selfDate = [self dateToDate:date formaterMode:NSDateFormaterModeDay];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    // 获得nowDate和selfDate的差距
    NSDateComponents *cmps = [calendar components:NSCalendarUnitDay fromDate:nowDate toDate:selfDate options:0];
    
    return cmps.day;
}

/*
 * 需要传入的时间格式 2017-06-14 14:18:54    暂不考虑跨年
 * 时间显示规则
 * 当天       时:分
 * 前一天      昨天
 * 其余       月:日
 */
#pragma mark - 时间显示规则
+(NSString *)inputTimeStr:(NSString *)timeStr{
    
    //目标日期
    NSDate *sinceDate = [self stringToDate:timeStr formaterMode:NSDateFormaterModeDefault];
    
    if ([self isTodayWithDate:sinceDate]) {
        //今天 - 时:分
        return [NSString stringWithFormat:@"%.2ld:%.2ld",sinceDate.hour,sinceDate.minute];
    }else if([self isYesterdayWithDate:sinceDate]){
        //昨天 - 昨天
        return [NSString stringWithFormat:@"昨天"];
    }else{
        //昨天以前 - 月:日
        return [NSString stringWithFormat:@"%.2ld月%.2ld日",sinceDate.month,sinceDate.day];
    }
}

#pragma mark - 计算时长 天 - 小时 - 分
+(NSString *)intervalSinceFromDate:(NSString *)fromDate toDate:(NSString *)toDate dateModel:(NSDateFormaterMode)dateModel{
    
    NSString *timeString=@"";
    
    NSDateFormatter *format=[NSDateFormatter dateFormaterWithMode:dateModel ? : NSDateFormaterModeTime];
    
    //获取开始时间
    NSDate *fromdate=[format dateFromString:fromDate];
    NSTimeZone *fromzone = [NSTimeZone systemTimeZone];
    NSInteger frominterval = [fromzone secondsFromGMTForDate: fromdate];
    NSDate *startDate = [fromdate dateByAddingTimeInterval: frominterval];
    
    //获取结束时间
    NSDate *todate = [format dateFromString:toDate];
    NSTimeZone *tozone = [NSTimeZone systemTimeZone];
    NSInteger tointerval = [tozone secondsFromGMTForDate:todate];
    NSDate *endDate = [todate dateByAddingTimeInterval:tointerval];
    
    //计算时长
    double intervalTime = [startDate timeIntervalSinceReferenceDate] - [endDate timeIntervalSinceReferenceDate];
    long lTime = labs((long)intervalTime);
    
    //秒差
    //NSInteger iSeconds =  lTime % 60;//秒
    //一小时内的分差
    NSInteger iMinutes = (lTime / 60) % 60;//分
    //一天内的小时差
    NSInteger iHours = labs(lTime/60/60) - labs(lTime/60/60/24) * 24;//时
    //天数差
    NSInteger iDays = labs(lTime/60/60/24);//天
    //NSInteger iMonth =lTime/60/60/24/12;//月
    //NSInteger iYears = lTime/60/60/24/384;
    
    if (iDays>0) {
        //大于一天
        timeString=[NSString stringWithFormat:@"%ld天%ld小时%ld分", iDays, iHours, iMinutes];
        
    }else{
        //小于一天
        
        if (iHours>0) {
            //大于1小时
            timeString=[NSString stringWithFormat:@"%ld时%ld分",iHours, iMinutes];
        }else{
            //小于1小时
            timeString=[NSString stringWithFormat:@"%ld分",iMinutes];
        }
    }
    
    return timeString;
}

#pragma mark - 时间戳转日期   10位
+(NSString *)timestampToDate:(NSString *)timestamp{
    
    // 13位时间戳
    NSInteger tamp = [timestamp integerValue];
    if (timestamp.length == 13) {
        tamp = tamp/1000;
    }
    
    // iOS 生成的时间戳是10位
    NSTimeInterval interval    = tamp;
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString       = [formatter stringFromDate: date];
    return dateString;
}

#pragma mark - 日期对比(NSDate)
+ (int)compareOneDate:(NSDate *)date1 withAnotherDate:(NSDate *)date2
{
    NSDate *dateA = [NSDate dateToDate:date1 formaterMode:NSDateFormaterModeDefault];
    NSDate *dateB = [NSDate dateToDate:date2 formaterMode:NSDateFormaterModeDefault];
    NSComparisonResult result = [dateA compare:dateB];
    
    if (result == NSOrderedDescending) {
        NSLog(@"%@  %@",dateB,dateA);
        return 1;
    }
    else if (result == NSOrderedAscending){
        NSLog(@"%@  %@",dateA,dateB);
        return -1;
    }
    NSLog(@"%@:%@ are the same",dateA,dateB);
    return 0;
}

#pragma mark - 日期对比(NSString)
+(int)compareOneDateString:(NSString *)date1 withAnotherDateString:(NSString *)date2{
    
    NSString *date1Str = [NSDate regularDateString:date1 formaterMode:NSDateFormaterModeDefault];
    NSString *date2Str = [NSDate regularDateString:date2 formaterMode:NSDateFormaterModeDefault];
    
    NSDateFormatter *formater = [NSDateFormatter dateFormaterWithMode:NSDateFormaterModeDefault];
    
    NSDate *dateA = [formater dateFromString:date1Str];
    NSDate *dateB = [formater dateFromString:date2Str];
    
    NSComparisonResult result = [dateA compare:dateB];
    
    if (result == NSOrderedDescending) {
        NSLog(@"%@  %@",dateB,dateA);
        return 1;
    }
    else if (result == NSOrderedAscending){
        NSLog(@"%@  %@",dateA,dateB);
        return -1;
    }
    NSLog(@"%@:%@ are the same",dateA,dateB);
    return 0;
}


//获取农历日期


+(NSDictionary *)getChineseCalendarWithDate:(NSDate *)date{
    
    NSArray *chineseYears = [NSArray arrayWithObjects:@"甲子",@"乙丑",@"丙寅",@"丁卯",@"戊辰",@"己巳",@"庚午",@"辛未",@"壬申",@"癸酉",@"甲戌",@"乙亥",@"丙子",@"丁丑",@"戊寅",@"己卯",@"庚辰",@"辛己",@"壬午",@"癸未",@"甲申",@"乙酉",@"丙戌",@"丁亥",@"戊子",@"己丑",@"庚寅",@"辛卯",@"壬辰",@"癸巳",@"甲午",@"乙未",@"丙申",@"丁酉",@"戊戌",@"己亥",@"庚子",@"辛丑",@"壬寅",@"癸丑",@"甲辰",@"乙巳",@"丙午",@"丁未",@"戊申",@"己酉",@"庚戌",@"辛亥",@"壬子",@"癸丑",@"甲寅",@"乙卯",@"丙辰",@"丁巳",@"戊午",@"己未",@"庚申",@"辛酉",@"壬戌",@"癸亥", nil];
    
    NSArray *chineseMonths=[NSArray arrayWithObjects:@"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月",@"九月", @"十月", @"冬月", @"腊月", nil];
    
    NSArray *chineseDays=[NSArray arrayWithObjects:@"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十",@"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十",@"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十",nil];
    
//    NSDate *dateTemp = nil;
//    NSDateFormatter *dateFormater = [[NSDateFormatter alloc]init];
//    [dateFormater setDateFormat:@"yyyy-MM-dd"];
    
//    dateTemp = [dateFormater dateFromString:date];
    
    NSCalendar *localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
    
    unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    NSDateComponents *localeComp = [localeCalendar components:unitFlags fromDate:date];
    //NSLog(@"%d_%d_%d  %@",localeComp.year,localeComp.month,localeComp.day, localeComp.date);
    
    NSString *y_str = [chineseYears objectAtIndex:localeComp.year-1];
    NSString *m_str = [chineseMonths objectAtIndex:localeComp.month-1];
    NSString *d_str = [chineseDays objectAtIndex:localeComp.day-1];
    
    NSDictionary *dict = @{@"year":y_str,@"month":m_str,@"day":d_str};
    
    //NSString *chineseCal_str =[NSString stringWithFormat: @"%@_%@_%@",y_str,m_str,d_str];
    
    return dict;
}

+ (NSInteger)minuteStartTime:(NSString *)startTime endTime:(NSString *)endTime{
    
    NSDateFormatter *date = [[NSDateFormatter alloc]init];
    
    [date setDateFormat:@"HH:mm"];
    
    NSDate *startD =[date dateFromString:startTime];
    
    NSDate *endD = [date dateFromString:endTime];
    
    NSTimeInterval start = [startD timeIntervalSince1970]*1;
    
    NSTimeInterval end = [endD timeIntervalSince1970]*1;
    
    NSTimeInterval value = end - start;
    
    int minute = (int)value / 60;
   
    return minute;
}

//计算时间差
+ (NSString *)dateInterval:(NSDate *)date time:(NSInteger)minute{
    //当前时间n分钟后
//    NSDate *locaDate = [NSDate date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *compents = [[NSDateComponents alloc] init];
    [compents  setMinute:minute];
    NSDate *minDate = [calendar dateByAddingComponents:compents toDate:date ? : [NSDate date] options:0];
    NSDateFormatter *originalFormater = [NSDateFormatter dateFormaterWithMode:NSDateFormaterModeDefault];
    NSString *startTime = [originalFormater stringFromDate:minDate];
    return startTime;
}

//获取当前时间戳  (以秒为单位)
+(NSString *)getNowTimeTimestamp{
    
    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/Shanghai"];
    [formatter setTimeZone:timeZone];
    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
    return timeSp;
}

//获取当前时间戳  (以毫秒为单位)
+(NSString *)getNowTimeTimestamp3{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss SSS"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    //设置时区,这个对于时间的处理有时很重要
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]*1000];
    return timeSp;
}

//将本地日期字符串转为UTC日期字符串
//本地日期格式:2013-08-03 12:53:51
//可自行指定输入输出格式
+(NSString *)getUTCFormateLocalDate:(NSString *)localDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //输入格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *dateFormatted = [dateFormatter dateFromString:localDate];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    [dateFormatter setTimeZone:timeZone];
    //输出格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
    NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
    
    return dateString;
}

//将UTC日期字符串转为本地时间字符串
//输入的UTC日期格式2013-08-03T04:53:51+0000
+(NSString *)getLocalDateFormateUTCDate:(NSString *)utcDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //输入格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
    NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
    [dateFormatter setTimeZone:localTimeZone];
    
    NSDate *dateFormatted = [dateFormatter dateFromString:utcDate];
    //输出格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
    
    return dateString;
}

#pragma mark - 设置date - second
-(void)setSecond:(NSInteger)second{
    
}

#pragma mark - 设置date - year
-(void)setYear:(NSInteger)year{
    
}

#pragma mark - 设置date - day
-(void)setMonth:(NSInteger)month{
    
}

#pragma mark - 设置date - day
-(void)setDay:(NSInteger)day{
    
}

#pragma mark - 设置date - hour
-(void)setHour:(NSInteger)hour{
    
}

#pragma mark - 设置date - minute
-(void)setMinute:(NSInteger)minute{
    
}

#pragma mark - 设置weekmonth - weekmonth
-(void)setWeekmonth:(NSInteger)weekmonth{
    
}

#pragma mark - 设置weekyear - weekyear
-(void)setWeekyear:(NSInteger)weekyear{
    
}

#pragma mark - 设置week - week
-(void)setWeekday:(NSInteger)weekday{
    
}

-(void)setYesterday:(NSString *)yesterday{
    
}

-(void)setTomorrow:(NSString *)tomorrow{
    
}

-(void)setWeekStr:(NSString *)weekStr{
    
}

-(void)setDayAfterTomorrow:(NSString *)dayAfterTomorrow{
    
}

@end

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,294评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,493评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,790评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,595评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,718评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,906评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,053评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,797评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,250评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,570评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,711评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,388评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,018评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,796评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,023评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,461评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,595评论 2 350