0

Incorrect day labels when week starts on Monday. Using the app with Norwegian locale, the week is detected and graphed correctly starting from Monday but labels still start from Sunday. The current week identified as "25.-31. mai" is therefore labeled as "søn. man. tir. ..." (Sun Mon Tue...) instead of "man. tir. ons. ..." (Mon Tue Wed ...). How to fix this?

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"))
}
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Damian
  • 215
  • 1
  • 3
  • 8
  • 1
    `(NS)Calendar` has a `firstWeekday` property which gives you the first day of the week according to your locale (e.g. 0=Sunday or 1=Monday), see http://stackoverflow.com/questions/27153013/swift-weekday-by-current-location-by-currentcalendar for an example. – Martin R Mar 23 '17 at 20:54
  • And do not use 86400 second as the duration of a day – that is wrong on days with daylight saving transitions. – Martin R Mar 23 '17 at 21:00
  • Thanks, but how better to do this because I have no idea... – Damian Mar 23 '17 at 21:11
  • 1
    You already have the beginning of the week. Just start with that date and add 1 day in a loop. Compare http://stackoverflow.com/a/32536738/1187415. – Martin R Mar 23 '17 at 21:14
  • Martin, thank you! – Damian Mar 23 '17 at 21:20

0 Answers0