title: CoreData for storing dates recommended practices
body: <p>I'm making a simple shift tracker / salary calculator to practice CoreData and Swift. I'm having issues with the different tasks related to this, as I need to use the dates to: </p>
  
  <ul>
  <li>Display them</li>
  <li>Check amount of shifts in a week to see if you deserve extra pay (Norwegian rules gives you 5/10$+ extra per hour if you work more than 12 hours that week) </li>
  <li>Add all salaries in a month together</li>
  <li>Delete data if the user types in wrong</li>
  </ul>
  
  <p>And probably more i'm forgetting. </p>
  
  <p>As of right now, I'm getting the data from date pickers which use a date formatter to add them to by DB as 'fromDate' and 'toDate' as a string. This leads to me having to use a bunch of bad code. Much because of NSDate timezone issues etc. </p>
  
  <p>How should I lay out my data base to do this most effectively? Should I make a field for each type of data I need? ('fromMinute'|'fromHour'|'fromDay'|'fromWeek'|'fromMonth'|'fromYear') and then corresponding for 'to' fields? I'm a rookie at databases but I don't think I should have that many fields. Should I have a month table, then a week table, then use relations (and in that case how do I do that?) </p>
  
  <p>I can not just use a Time Interval, because bonuses are dependent on how late it is. </p>
  
  <p>Hope someone can help me out with some recommended practices on CoreData</p>
  
  <hr>
  
  <p>UPDATE:</p>
  
  <p>Here is my super complex core data model! 
  <img src="https://i.stack.imgur.com/aVPVh.png" alt="enter image description here"></p>
  
  <p>along with a NSManagedObject class ( I think ) </p>
  
  <pre><code>import UIKit
  import CoreData
  
  @objc(Shifts)
  
  class Shifts: NSManagedObject {
      @NSManaged var fromDate : String
      @NSManaged var toDate : String
  
      func getShift () -&gt; Shift{
          let formatter = NSDateFormatter()
          formatter.dateFormat = "yyyy-MM-dd w W HH:mm"
          let from:NSDate = formatter.dateFromString(self.fromDate)!
          let to:NSDate = formatter.dateFromString(self.toDate)!
          let length:Double = to.timeIntervalSinceDate(from) as Double / 60
  
          let newShift = Shift(from:from,to:to,length:length)
          return newShift
      }
  
  
  }
  </code></pre>
  
  <p>Shit is a class I have in my view controller which I use to display etc my data, it looks like this (TW:Code Gore) </p>
  
  <pre><code>class Shift {
  var fromDate : NSDate
  var toDate : NSDate
  var length : Double //In Minutes
  var salary : Double = Double()
  var UB : Bool = false
  
  let etter18hverdag:Double = 22
  let etter21hverdag:Double = 45
  let helligdag:Double = 90
  let helgEtter13:Double = 45
  let helgEtter16:Double = 90 //HUSK AT PAUSE FINNES
  
  
  init (from : NSDate, to : NSDate, length:Double){
      self.fromDate = from
      self.toDate = to
      self.length = length
  }
  func calcSalary(ub: Bool)-&gt;Double{
  
      let userDefaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
      let hour: Double = userDefaults.doubleForKey("salary")
  
      println("Checking salary on shift fromDate:\(fromDate) with length:\(length). Shift UB: \(UB)")
  
      if (ub){
          let calendar : NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)
          calendar.locale = NSLocale.systemLocale()
          calendar.timeZone = NSTimeZone.localTimeZone()
  
          var checkTime:NSDate = fromDate
          var checkToTime:NSDate = toDate
  
          var totalSalary:Double = Double()
  
          while (checkToTime.timeIntervalSinceDate(checkTime)&gt;0.0){
  
              let split = calendar.components(NSCalendarUnit.WeekdayCalendarUnit | NSCalendarUnit.HourCalendarUnit, fromDate: checkTime)
  
              println("Checking: \(checkTime) to: \(checkToTime) diff:\(checkToTime.timeIntervalSinceDate(checkTime)) currentSal: \(totalSalary) splitTime:\(split.hour)")
              if(split.weekday==7){ //Søndag
                  let lønn = hour + helligdag
                  totalSalary += (lønn/4)
              }
  
              else if (split.weekday == 6){ //Lørdag
                  if(split.hour &lt; 13){
                      let lønn = hour
                      totalSalary += (lønn/4)
                  }
                  if (split.hour&gt;13 &amp;&amp; split.hour&lt;16){
                      let lønn = hour + helgEtter13
                      totalSalary += (lønn/4)
  
                  }
                  if (split.hour &gt; 16){
                      let lønn = hour + helgEtter16
                      totalSalary += (lønn/4)
                  }
  
              }
  
              else if (split.weekday &lt; 6){ //Hverdag
                  if(split.hour &lt; 18){
                      let lønn = hour
                      totalSalary += (lønn/4)
  
                  }
                  if (split.hour&gt;18 &amp;&amp; split.hour&lt;21){
                      let lønn = hour + etter18hverdag
                      totalSalary += (lønn/4)
  
                  }
                  if (split.hour &gt; 21){
                      let lønn = hour + etter21hverdag
                      totalSalary += (lønn/4)
                  }
              }
  
  
              checkTime = checkTime.dateByAddingTimeInterval(15*60) //15 min ganger 60 sek i min
          }
          println("Calculated salary WITH UB: \(totalSalary)")
  
          return totalSalary
      } else {
          let calendar : NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)
          calendar.locale = NSLocale.systemLocale()
          calendar.timeZone = NSTimeZone.localTimeZone()
  
          var checkTime:NSDate = fromDate
          var totalSalary:Double = Double()
          let multiple = (length/60)
  
          while (self.toDate.timeIntervalSinceDate(checkTime)&gt;0.0){
  
              let lønn = hour
              totalSalary +=  (lønn/4)
  
              checkTime = checkTime.dateByAddingTimeInterval(15*60) //15 min ganger 60 sek i min
          }
          println("Calculated salary WITHOUT UB: \(totalSalary)")
          return totalSalary
      }
  }
  </code></pre>