自定义字符串转换为周几和指定日期格式

时间:2022-07-01 03:01:00
日期转换为今天
完全自定义绝对日期数字字符串转化为指定日期格式
自定义绝对日期数字字符串转化为今天,明天,昨天,周几

NSString+Extension.h

#import <Foundation/Foundation.h>
@interface NSString (Extension)
- (NSString *)currentDate;
- (NSString *)getWeekDate;
@end

NSString+Extension.m

#import "NSString+Extension.h"
@implementation NSString (Extension)
-(BOOL)leapYear : (long)currentday
{
if(currentday % 400 == 0)
{
return YES;
}
else if((currentday % 100 != 0) && (currentday % 4 == 0))
{
return YES;
}
else
{
return NO;
}
}
#pragma mark - 把绝对时间转换为年/月/日格式的日期字符串
-(NSString *)currentDate
{
if([self longLongValue] < 0)
{
return @"未知时间";
}

long long dateDistace = ([self longLongValue]/1000 + 8*3600)/86400;
long long n = 0;
long i,j,k,m;
long month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
NSMutableDictionary *dic = [self getStartDayWithminDistace:dateDistace];
i = [dic[@"startDay"] longValue];
n = [dic[@"startValue"] longLongValue];
for(j = 1, k = 1; (n <= dateDistace) && (i < 3000); i++)
{
for(j = 1; j <= 12 ; j++)
{
m = month[j - 1];
for(k = 1; k <= m; k++)
{
n++;
if(n == dateDistace)
{
return [NSString stringWithFormat:@"%ld/%ld/%ld", i, j,k];
}
}

if((j == 2) && [ self leapYear: i])
{
n++;
if(n == dateDistace)
{
return [NSString stringWithFormat:@"%ld/%ld/%ld", i, j,k];
}
}
}
}

return nil;
}

#pragma mark - 把绝对时间转换为今天,昨天,明天,周几
-(NSString *)getWeekDate
{
if([self longLongValue] < 0)
{
return @"未知时间";
}
long long nowTime = (long long)[[NSDate date] timeIntervalSince1970] + 8*3600;
// //测试代码
// long long minDistace = ([self longLongValue]/1000 + 8*3600 + 1*24*3600)/86400;
//注意Java服务器获取到的时间是精确到毫秒的,苹果获取到的时间默认是精确到秒,若想得到毫秒需要乘以1000,反之要除以1000;获取到的绝对时间都是以零时区的时间为准,所以北京要加八个小时时间
long long minDistace = ([self longLongValue]/1000 + 8*3600)/86400;
long long maxDistace = nowTime/86400;
BOOL isOverNow = minDistace > maxDistace;
if(isOverNow)
{
long long temp = minDistace;
minDistace = maxDistace;
maxDistace = temp;
}

long long n = 0;
long i,j,k,m,interval;
interval = 0;
long month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
NSMutableDictionary *dic = [self getStartDayWithminDistace:minDistace];
i = [dic[@"startDay"] longValue];
n = [dic[@"startValue"] longLongValue];
for(j = 1, k = 1; (n <= maxDistace) && (i < 3000); i++)
{
for(j = 1; j <= 12 ; j++)
{
m = month[j - 1];
for(k = 1; k <= m; k++)
{
n++;
if(n == minDistace)
{
interval++;
}

if(n == maxDistace)
{
return [self getWeekDatValueWithIsOverNow:isOverNow interval:interval];
}
else if(interval > 0)
{
interval++;
}
}

if((j == 2) && [ self leapYear: i])
{
n++;
if(n == minDistace)
{
interval++;
}

if(n == maxDistace)
{
return [self getWeekDatValueWithIsOverNow:isOverNow interval:interval];
}
else if(interval > 0)
{
interval++;
}
}
}
}

return nil;
}

-(NSMutableDictionary *)getStartDayWithminDistace:(long long)minDistace
{
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
if(minDistace >= 17167)
{
[dic setSafeObject:@"2017" forKey:@"startDay"];
[dic setSafeObject:@"17167" forKey:@"startValue"];
}
else if(minDistace >= 16436)
{
[dic setSafeObject:@"2015" forKey:@"startDay"];
[dic setSafeObject:@"16436" forKey:@"startValue"];
}
else if(minDistace >= 14610)
{
[dic setSafeObject:@"2010" forKey:@"startDay"];
[dic setSafeObject:@"14610" forKey:@"startValue"];
}
else if(minDistace >= 10957)
{
[dic setSafeObject:@"2000" forKey:@"startDay"];
[dic setSafeObject:@"10957" forKey:@"startValue"];
}
else
{
[dic setSafeObject:@"1970" forKey:@"startDay"];
[dic setSafeObject:@"0" forKey:@"startValue"];
}
return dic;
}

-(NSString *)getWeekDatValueWithIsOverNow:(BOOL)isOverNow
interval:(NSInteger)interval
{
NSDate *inputDate = [NSDate date];
NSArray *weekdays = [NSArray arrayWithObjects:[NSNull null],@"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];

[calendar setTimeZone: timeZone];

NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
/*注意经过测试,就是你把这个日期(inputDate)置为以前的时间,通过[calendar components:calendarUnit fromDate:inputDate]你得到的theComponents.weekday也是今天所的周几,如:今天是周五,theComponents.weekday为5,和inputDate无关。所以只有你自己实现和今天的差的天数来确定具体哪天是周几才正确*/
NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
NSLog(@"inputDate:%@, --%@", inputDate, [weekdays objectAtIndex:theComponents.weekday]);

if(interval == 1)
{
return @"今天";
}
else if(interval == 2)
{
if(isOverNow)
{
return @"明天";
}
else
{
return @"昨天";
}
}
else
{
NSInteger weekdayIndex = theComponents.weekday;
NSInteger num = (interval - 1)/7 + 1;
if(isOverNow)
{
weekdayIndex = (num*7 + weekdayIndex + (interval - 1))%7;
}
else
{
weekdayIndex = (num*7 + weekdayIndex - (interval - 1))%7;
}

if(weekdayIndex == 0)
{
weekdayIndex = 7;
}
return [weekdays objectAtIndex:weekdayIndex];
}
}
@end

使用例子:
先包含头文件

#import "NSString+Extension.h"

代转换字符串如:@”1490215716000”
如你的时间是精确到秒的请先乘以1000后转化为字符串调用,因为java服务器保存的时间默认都是精确到毫秒,而苹果系统默认都是精确到秒,毫秒数都是在小数点后。
使用时调用

    NSString *time = [NSString stringWithFormat:@"%lld", (long long)[[NSDate date] timeIntervalSince1970]*1000];
NSLog(@"日期:%@", [time getWeekDate]);