//
//  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
