Date from week of year returning date not in week

Deadly 提交于 2019-12-13 10:50:29

问题


I have come across a rather strange "bug". When getting a date for a week of a year using this method:

let dates = NSMutableArray()
let cal = NSCalendar.currentCalendar()
cal.firstWeekday = 2
let formatter = NSDateFormatter()
formatter.dateFormat = "ww YYYY"
formatter.calendar = cal
let date = formatter.dateFromString(week as String)
println(date)

The string week is 52 2014, so the expected date would be Monday December 22th, but instead it returns Saturday December 20th, at 23:00. First of all, I thought I'd handled the first day of week by setting the firstWeekday-option of the calendar, but no luck. In addition, the date returned isn't even in week 52.

Just to double check I ran cal.components(NSCalendarUnit.WeekOfYearCalendarUnit, fromDate: date!).weekOfYear to double check I'm not an idiot, and no sir, the week for the date produced is 51, the week before the desired week.

Any idea how I can reach the expected result?


回答1:


Changing the firstWeekday from 1 to 2 won't change the date, it will change just the First weekday from Sunday to Monday.

You can do it like this:

func dateFromWeekOfYear(year:Int, weekOfYear:Int, weekday:Int) -> NSDate {
    return NSCalendar.currentCalendar().dateWithEra(1, yearForWeekOfYear: year, weekOfYear: weekOfYear, weekday: weekday, hour: 0, minute: 0, second: 0, nanosecond: 0)!
}

let date1 = dateFromWeekOfYear(2014, 52, 1)   // Dec 21, 2014, 12:00 AM
let date2 = dateFromWeekOfYear(2014, 52, 2)   // Dec 22, 2014, 12:00 AM
let date3 = dateFromWeekOfYear(2014, 52, 3)   // Dec 23, 2014, 12:00 AM

If dealing with a string and you want to set he Stand Alone local day of week you can do it like this:

let myDate = "2 52 2014"
let cal = NSCalendar.currentCalendar()
let formatter = NSDateFormatter()
formatter.dateFormat = "c ww Y"
formatter.calendar = cal
if let date1 = formatter.dateFromString(myDate) {
    date1  // "Dec 22, 2014, 12:00 AM"
}

If you need further reference you can use this:




回答2:


Any idea how I can reach the expected result?

What actually is your desired result? Do you want to know the first day of the week or the first day in the last day? Than you could tray this:

let now = NSDate()
var startDate: NSDate? = nil
var duration: NSTimeInterval = 0

let cal = NSCalendar.currentCalendar()
cal.firstWeekday = 2

cal.rangeOfUnit(.WeekCalendarUnit, startDate: &startDate, interval: &duration, forDate: now);


let endDate = startDate?.dateByAddingTimeInterval(duration)
print(startDate)
print(endDate)

it prints

"Optional(2014-12-21 23:00:00 +0000)"
"Optional(2014-12-28 23:00:00 +0000)"

the endDate is the first second that is not in the week anymore.

Note that the offset of 1 hour results from the fact that it is printed in UTC time, that is actually GMT winter time. Indeed these dates are 2014-12-22 00:00:00 and 2014-12-29 00:00:00 in my time zone (GMT+1)


or simply

let components = NSDateComponents()
components.weekOfYear = 52
components.weekday = 2
components.year = 2014

let cal = NSCalendar.currentCalendar()    
let day = cal.dateFromComponents(components)

This code adapted to respect user's calendar:

let cal = NSCalendar.currentCalendar()
let components = NSDateComponents()
components.weekOfYear = 52
components.weekday = cal.firstWeekday
components.year = 2014


来源:https://stackoverflow.com/questions/27651727/date-from-week-of-year-returning-date-not-in-week

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!