将年中的日期转换为日期

时间:2013-12-25 07:09:25

标签: ios iphone objective-c

目前我无法将年份中的某一天整数转换为格式化的字符串/日期,例如 mm-dd 。我尝试了一些不同的解决方案方法,包括这里发布的大多数解决方案 - 没有成功。

我希望如此:今天是 359 天 - 它应该转换为 12-25

2 个答案:

答案 0 :(得分:5)

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents* components = [[NSDateComponents alloc] init];
[components setDay:359];
[components setYear:2013];
[components setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate* day359 = [gregorian dateFromComponents:components];

输出

2013-12-25 00:00:00 +0000

答案 1 :(得分:1)

真的吗?即使不知道目标-c它似乎也很直接:

const JANUARY = 31;
const FEBURARY = JANUARY + 28;
const MARCH = FEBRUARY + 31;
...
const NOVEMBER = OCTOBER + 30;

if( day < JANUARY ) { dd = day; mm = 1; }
else if( day < FEBRUARY ) { dd = day - JANUARY; mm = 2 }
else if( day < MARCH ) { dd = day - FEBRUARY; mm =  3 }
...
else if( day < NOVEMBER ) { dd = day - OCTOBER; mm = 11 }
else { dd == day - NOVEMBER; mm = 12 }

print "$dd/$mm";