96 lines
2.7 KiB
Objective-C
96 lines
2.7 KiB
Objective-C
//
|
|
// NSDate+DateUtils.m
|
|
// xplan-ios
|
|
//
|
|
// Created by 冯硕 on 2022/4/12.
|
|
//
|
|
|
|
#import "NSDate+DateUtils.h"
|
|
|
|
|
|
NSString * const kDateFormatYYYYMMDD = @"yyyy-MM-dd";
|
|
NSString * const kDateFormatYYMMDDTHHmmss = @"yyyy-MM-dd'T'HH:mm:ss";
|
|
|
|
@implementation NSDate (DateUtils)
|
|
+ (NSDateFormatter *)shareDateFormatter {
|
|
static NSDateFormatter *currentFormatter = nil;
|
|
static dispatch_once_t sharedDateFormatter_onceToken;
|
|
dispatch_once(&sharedDateFormatter_onceToken, ^{
|
|
if (!currentFormatter) {
|
|
currentFormatter = [[NSDateFormatter alloc] init];
|
|
[currentFormatter setDateFormat:kDateFormatYYYYMMDD];
|
|
[currentFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
|
|
}
|
|
});
|
|
return currentFormatter;
|
|
}
|
|
|
|
- (NSDate *)dateByAddingDays:(NSInteger)days {
|
|
NSCalendar *calendar = [NSCalendar sharedCalendar];
|
|
NSDateComponents *components = [[NSDateComponents alloc] init];
|
|
[components setDay:days];
|
|
return [calendar dateByAddingComponents:components toDate:self options:0];
|
|
}
|
|
|
|
- (NSInteger)daysBetween:(NSDate *)aDate {
|
|
NSUInteger unitFlags = NSCalendarUnitDay;
|
|
NSDate *from = [NSDate at_dateFromString:[self stringForYearMonthDayDashed]];
|
|
NSDate *to = [NSDate at_dateFromString:[aDate stringForYearMonthDayDashed]];
|
|
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
|
|
NSDateComponents *components = [calendar components:unitFlags fromDate:from toDate:to options:0];
|
|
return labs([components day]) + 1;
|
|
}
|
|
|
|
- (NSString *) stringForYearMonthDayDashed {
|
|
return [self stringForFormat:kDateFormatYYYYMMDD];
|
|
}
|
|
+(NSString *)getNowTimeTimestamp{
|
|
|
|
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
|
|
|
|
NSTimeInterval a=[dat timeIntervalSince1970];
|
|
|
|
NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型
|
|
|
|
return timeString;
|
|
}
|
|
- (NSString *)stringForFormat:(NSString *)format {
|
|
if (!format) {
|
|
return nil;
|
|
}
|
|
|
|
NSDateFormatter *formatter = [NSDate shareDateFormatter];
|
|
[formatter setDateFormat:format];
|
|
|
|
NSString *timeStr = [formatter stringFromDate:self];
|
|
return timeStr;
|
|
}
|
|
|
|
+ (NSDate *)at_dateFromString:(NSString *)dateString {
|
|
NSDateFormatter *dateFormatter = [self shareDateFormatter];
|
|
if (dateString.length > kDateFormatYYYYMMDD.length) {
|
|
[dateFormatter setDateFormat:kDateFormatYYMMDDTHHmmss];
|
|
} else {
|
|
[dateFormatter setDateFormat:kDateFormatYYYYMMDD];
|
|
}
|
|
NSDate *date = [dateFormatter dateFromString:dateString];
|
|
if (!date) {
|
|
date = [NSDate date];
|
|
}
|
|
return date;
|
|
}
|
|
@end
|
|
|
|
@implementation NSCalendar (Pick)
|
|
|
|
+ (instancetype)sharedCalendar
|
|
{
|
|
static id instance;
|
|
static dispatch_once_t onceToken;
|
|
dispatch_once(&onceToken, ^{
|
|
instance = [NSCalendar currentCalendar];
|
|
});
|
|
return instance;
|
|
}
|
|
@end
|