1

I am looking to have a label that is shown and hidden depending on the time of day. I am using this as a type of open sign at a store. Between the hours of 8 am - 5 pm Mon - Fri, the label displays, if not, the label is hidden. I can't exactly figure out how to compare to the current time and open hours in order to carry out the if/else statements. Also, I do not have an idea about the date but assume would be easy if the previous problem was solved.

Here is the code that I have. I did not add much becuase I know what I have is not going to work with the mktClock.stringValue >= "Time".

    //Set time
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "hh:mm:ss a zzz"
    dateFormatter.timeZone = NSTimeZone(name: "US/Eastern")
    dateString = dateFormatter.stringFromDate(date)
    mktClock.stringValue = dateString as String

    //Displays open if the market is open
    if mktClock.stringValue >= "09:30:00 AM EDT" {
        mktOpen.hidden = false
    }else{
        mktOpen.hidden = true
    }
Andrew
  • 13
  • 4
  • Take a look at [NSDate#compare](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/#//apple_ref/doc/uid/20000188-SW6), along with [NSDateComponents](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateComponents_Class/) (to construct the open hours). – Dominic K Jun 16 '16 at 22:36

2 Answers2

1

Try this:

func getDayOfWeek() -> Int {
    let todayDate = NSDate()
    let myCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
    let myComponents = myCalendar?.components(.Weekday, fromDate: todayDate)
    let weekDay = myComponents?.weekday
    return weekDay!
}

Credit: https://stackoverflow.com/a/28866064/5143847

Now to get your solution:

let dayOfWeek = getDayOfWeek()
if dayOfWeek > 1 && dayOfWeek < 7 {
    let hour = NSCalendar.currentCalendar().component(.Hour, fromDate: NSDate())
    if hour > 8 && hour < 13 {
        mktOpen.hidden = false
    } else {
        //Before 8 or after 5
        mktOpen.hidden = true
    }
} else {
    //Sunday or Saturday
    mktOpen.hidden = true
}

let hour credit:

https://stackoverflow.com/a/27547225/5143847

Community
  • 1
  • 1
Pranav Wadhwa
  • 7,666
  • 6
  • 39
  • 61
  • Well done, It seems to be working so far but still need to test it with the time and see if it changes. Im curious to see if the time is based off my defined US/Eastern time or if I need to restate that somewhere again. Im not sure yet it goes back off system time again. – Andrew Jun 17 '16 at 01:23
  • I have added in the time zone change to eastern so that way the clock does not go off anyone's system time. However, I am stuck on trying to figure out how to change an open or close time if the time does not land on a round hour. For example, how would the sign close if the closing time was not till 5:30 instead of 5? – Andrew Jun 17 '16 at 23:40
0

For anyone else who is looking into this and wants to set the time to work off a time zone, add this:

    //Set marketTime for open/close sign to US/Eastern Time in hours
    let mktFormatter = NSDateFormatter()
    mktFormatter.dateFormat = "HH" //Displays time in 24 hr format
    mktFormatter.timeZone = NSTimeZone(name: "US/Eastern")
    mktInt = mktFormatter.stringFromDate(date)
    let mktTime:Int! = Int(mktInt)!

To add minutes:

    //set marketTime2 for open/close sign in minutes
    let mktFormatter2 = NSDateFormatter()
    mktFormatter2.dateFormat = "mm" //Displays time in minutes
    mktFormatter2.timeZone = NSTimeZone(name: "US/Eastern")
    mktInt2 = mktFormatter2.stringFromDate(date)
    let mktTime2:Int! = Int(mktInt2)!

in this case, my mktTime = hours (24 hr format), and my mktTime2 = minutes.

Then just construct your if/else statements like penatheboss has above!

Andrew
  • 13
  • 4