Files
real-e-party-iOS/YuMi/Tools/Date/NSDate+DateUtils.m
edwinQQQ a35a711be6 chore: Initial clean commit
- Removed YuMi/Library/ (138 MB, not tracked)
- Removed YuMi/Resources/ (23 MB, not tracked)
- Removed old version assets (566 files, not tracked)
- Excluded Pods/, xcuserdata/ and other build artifacts
- Clean repository optimized for company server deployment
2025-10-09 16:19:14 +08:00

194 lines
6.8 KiB
Objective-C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// NSDate+DateUtils.m
// YUMI
//
// Created by YUMI 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];
}
- (NSDate *)dateByAddingSeconds:(NSInteger)seconds {
NSCalendar *calendar = [NSCalendar sharedCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setSecond:seconds];
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 *)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;
}
+ (NSString *)stringFromDate:(NSDate *)date {
NSDateFormatter * currentFormatter = [[NSDateFormatter alloc] init];
if ([self isDateInToday:date]) {
[currentFormatter setDateFormat:@"HH:mm"];
} else {
if (date.timeIntervalSince1970 > 0) {
[currentFormatter setDateFormat:YMLocalizedString(@"DateUtils0")];
} else{
[currentFormatter setDateFormat:YMLocalizedString(@"DateUtils1")];
}
}
NSString *dateString = [currentFormatter stringFromDate: date];
return dateString;
}
+(BOOL)isDateInToday:(NSDate *)date{
NSTimeInterval secondsPerDay = 24 * 60 * 60;
NSDate *today = [[NSDate alloc] init];
NSDate *tomorrow, *yesterday;
tomorrow = [today dateByAddingTimeInterval: secondsPerDay];
yesterday = [today dateByAddingTimeInterval: -secondsPerDay];
// 10 first characters of description is the calendar date:
NSString * todayString = [[today description] substringToIndex:10];
NSString * yesterdayString = [[yesterday description] substringToIndex:10];
NSString * tomorrowString = [[tomorrow description] substringToIndex:10];
NSString * dateString = [[date description] substringToIndex:10];
if ([dateString isEqualToString:todayString]){
return YES;
} else {
return NO;
}
}
+(NSString *)getNowTimeTimestamp{
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval a=[dat timeIntervalSince1970];
NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型
return timeString;
}
+(NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:format]; // @"YYYY-MM-dd hh:mm:ss"----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
return confromTimespStr;
}
#pragma mark - 将某个时间戳转化成 时间
+ (NSString *)timestampSwitchTime:(NSInteger)timestamp formatter:(NSString *)format{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:format]; // @"YYYY-MM-dd hh:mm:ss"----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
/// 在当前时间后退30分钟 = 1800
// confromTimesp = [confromTimesp dateByAddingTimeInterval:-1800];
NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
return confromTimespStr;
}
+ (NSInteger)pleaseInsertStarTimeo:(NSString *)time1 andInsertEndTime:(NSString *)time2{
// 1.将时间转换为date
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"YYYY-MM-dd hh:mm:ss";
NSDate *date1 = [formatter dateFromString:time1];
NSDate *date2 = [formatter dateFromString:time2];
// 2.创建日历
NSCalendar *calendar = [NSCalendar currentCalendar];
// NSCalendarUnit type = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSCalendarUnit type = NSCalendarUnitSecond;
// 3.利用日历对象比较两个时间的差值
NSDateComponents *cmps = [calendar components:type fromDate:date1 toDate:date2 options:0];
// 4.输出结果
// NSLog(@"两个时间相差%ld年%ld月%ld日%ld小时%ld分钟%ld秒", cmps.year, cmps.month, cmps.day, cmps.hour, cmps.minute, cmps.second);
return cmps.second;
}
+ (NSString *)getCurrentTimeWithFormat:(NSString *)format{
NSDate *now = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = format;
NSString *dateStr = [formatter stringFromDate:now];
return dateStr;
}
@end
@implementation NSCalendar (Pick)
+ (instancetype)sharedCalendar
{
static id instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [NSCalendar currentCalendar];
});
return instance;
}
@end