星期一星期开始时的日标签不正确。使用具有挪威语区域设置的应用程序,从星期一开始检测星期并正确绘制图表,但标签仍然从星期日开始。因此,本周被确定为“25.-31。mai”被标记为“søn.man.ti。...”(Sun Mon Tue ...),而不是“man.ti。ons .......”(星期一星期三...)。如何解决这个问题?
import UIKit
class STPYDateHelper: NSObject {
/**
Gets the day abbreviations for the week
:returns: The array of abbreviations
*/
class func dayAbbreviations() -> [String] {
let calendar = NSCalendar.currentCalendar()
calendar.timeZone = NSTimeZone.systemTimeZone()
var comps = NSDateComponents()
comps.day = 21
comps.month = 12
comps.year = 2014
var date = calendar.dateFromComponents(comps)
var strings = [String]()
for var day = 0; day < 7; day++ {
strings.append(STPYFormatter.sharedInstance.string(date!, format: "eee"))
date = date?.nextDay()
}
return strings
}
}
extension NSDate {
/**
Gets the next days date
:returns: The date
*/
func nextDay() -> NSDate {
return dateByAddingTimeInterval(86400)
}
/**
Gets the last date of the previous day
:returns: The date
*/
func endOfPreviousDay() -> NSDate {
return dateByAddingTimeInterval(-1)
}
/**
Gets the date for the beginning of the day
:returns: The date
*/
func beginningOfDay() -> NSDate {
let calendar = NSCalendar.currentCalendar()
calendar.timeZone = NSTimeZone.systemTimeZone()
var dateComps = calendar.components(.CalendarUnitYear | .CalendarUnitMonth | .CalendarUnitDay, fromDate: self)
dateComps.hour = 0
dateComps.minute = 0
dateComps.second = 0
return calendar.dateFromComponents(dateComps)!
}
/**
Gets the date for the beginning of the week
:returns: The date
*/
func beginningOfWeek() -> NSDate {
let calendar = NSCalendar.currentCalendar()
calendar.timeZone = NSTimeZone.systemTimeZone()
var date : NSDate?
var interval : NSTimeInterval = 0
calendar.rangeOfUnit(.CalendarUnitWeekOfYear, startDate: &date, interval: &interval, forDate: self)
if let date = date {
return date.beginningOfDay()
}
return NSDate()
}
/**
Gets the key for the beginning of the week
:returns: The key
*/
func beginningOfWeekKey() -> String {
return beginningOfWeek().key()
}
/**
Gets a Steppy date key for the date
:returns: The key
*/
func key() -> String {
return STPYFormatter.sharedInstance.string(self, format: "yyyy-MM-dd")
}
/**
Creates new NSDate instance from the Steppy date key
:param: key The Steppy date key
*/
convenience init(key : String) {
self.init(timeInterval:0, sinceDate:STPYFormatter.sharedInstance.date(key, format: "yyyy-MM-dd"))
}
}