iOS Get Date from Day of Year

后端 未结 1 837
广开言路
广开言路 2021-01-23 01:53

The common question on stackoverflow is how to get the Day of Year from a date but how to you get the date from the Day of Year?

I\'m using the following code to generat

相关标签:
1条回答
  • 2021-01-23 02:50

    Assuming you know the year you want and the actual time of the date doesn't matter and you have the correct locale and time zones set up, you can just convert the day of the year and year from components back into a date. Again, you may need to decide what your application needs to do about the time component and you have to make sure that the day and year actually make sense for the calendar, but assuming the gregorian calendar you listed in your example, and the 200th day of 2013 (19 July, 2013), you could so something like this:

    NSDateComponents* components = [[NSDateComponents alloc] init];
    [components setDay:200];
    [components setYear:2013];
    NSDate* july19_2013 = [gregorian dateFromComponents:components];
    

    If I were to run this, I'd get a date that's 19 July, 2013 @ 07:00 since my current locale is in a time zone equiv of America/Los_Angeles and I didn't configure the calendar.

    0 讨论(0)
提交回复
热议问题