NSDate+Additions.m 1.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
//
//  NSDate+Additions.m
//  HDMall
//
//  Created by Javen on 2017/7/31.
//  Copyright © 2017年 上海勾芒信息科技. All rights reserved.
//

#import "NSDate+Additions.h"

@implementation NSDate (Additions)

- (NSString *)httpParameterString {
    return [self stringWithFormatter:@"yyyy-MM-dd HH:mm:ss"];
}
- (NSString *)yearMonthDayString {
    return [self stringWithFormatter:@"yyyy-MM-dd"];
}
- (NSString *)yearMonthString {
    return [self stringWithFormatter:@"yyyy-MM"];
}

- (NSString *)yearString {
    NSString *strDate = [self yearMonthDayString];
    NSArray *arrDate = [strDate componentsSeparatedByString:@"-"];
    return arrDate[0];
}
- (NSString *)monthString {
    NSString *strDate = [self yearMonthDayString];
    NSArray *arrDate = [strDate componentsSeparatedByString:@"-"];
    return arrDate[1];
}
- (NSString *)dayString{
    NSString *strDate = [self yearMonthDayString];
    NSArray *arrDate = [strDate componentsSeparatedByString:@"-"];
    return arrDate[2];
}
- (NSString *)stringWithFormatter:(NSString *)dateFormatter {
    
    if ([dateFormatter length] == 0) {
        return nil;
    }
    
    // Change to Local time zone
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:self];
    NSDate *localDate = [self dateByAddingTimeInterval: interval];
    
    NSDateFormatter *f = [NSDateFormatter new];
    [f setDateFormat:dateFormatter];
    
    return [f stringFromDate:localDate];
}
@end