0

App Screenshot

I am trying to change the label that you see in my screenshot that has a green background and says We Are Open.

I would like the bottom label to turn RED and say "Sorry we are closed" whenever the listed opening times have passed and then go back to GREEN and say "We Are Open" at the correct opening times.

I've managed to import date and time successfully into the top label but I'm not sure how do the bottom label.

Here is the code:

import UIKit
import Firebase
import FirebaseInstanceID
import FirebaseMessaging

class FirstViewController: UIViewController {

    var timer = Timer()

    @IBOutlet weak var timeLabel: UILabel!

    @IBOutlet weak var openStatusLabel: UILabel!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        FIRMessaging.messaging().subscribe(toTopic: "/topics/news")

        self.timer = Timer.scheduledTimer(timeInterval: 1.0,
                                                            target: self,
                                                            selector: #selector(FirstViewController.tick),
                                                            userInfo: nil,
                                                            repeats: true)
    }

    @objc func tick() {
        timeLabel.text = DateFormatter.localizedString(from: NSDate() as Date,
                                                               dateStyle: .medium,
                                                               timeStyle: .medium)
    }

}
halfer
  • 19,824
  • 17
  • 99
  • 186
Elfuthark
  • 261
  • 1
  • 4
  • 17

1 Answers1

1

Below code i have try and it will work fine. Initially i created two UILabel with proper constraints. Then Create outlets for two label to view controller. Then try this code.

import UIKit

extension NSDate {
    func dayOfWeek() -> Int? {
        guard
            let calender: NSCalendar = NSCalendar.currentCalendar(),
            let component: NSDateComponents = calender.components(.Weekday, fromDate: self) else { return nil }
        return component.weekday
    }
}

class ViewController: UIViewController {


    @IBOutlet var timeOfTheDay: UILabel! //Top Label for showing current time

    @IBOutlet var Status: UILabel!  //Status Label for showing open or close

    override func viewDidLoad() {
        super.viewDidLoad()

        self.dateCheck()



    }

    func dateCheck()
    {

        let today = NSDate().dayOfWeek()

        if today == 1
        {
            //print("Sunday")
            self.closed()

        }
        else if today == 2
        {
            //print("Monday")
            self.closed()
        }
        else if today == 3
        {
            //print("Tuesday")
            self.uptoEvening()
        }
        else if today == 4
        {
            //print("Wednesday")
            self.uptoNight()
        }
        else if today == 5
        {
          //  print("Thursday")
            self.uptoNight()

        }
        else if today == 6
        {
            //print("Friday")
            self.uptoNight()

        }
        else
        {
            //print("Saturday")
            self.uptoEvening()
        }
    }

    func getTime() -> (hour:Int, minute:Int, second:Int) {
        let currentDateTime = NSDate()
        let calendar = NSCalendar.currentCalendar()
        let component = calendar.components([.Hour,.Minute,.Second], fromDate: currentDateTime)
        let hour = component.hour
        let minute = component.minute
        let second = component.second
        return (hour,minute,second)
    }


    func closed()
    {
        timeOfTheDay.text = String(getTime().hour)+" : "+String(getTime().minute)+" : "+String(getTime().second)
        timeOfTheDay.backgroundColor = UIColor.redColor()
        timeOfTheDay.textColor = UIColor.whiteColor()

        Status.text = "Sorry! Today, We are Closed!"
        Status.backgroundColor = UIColor.redColor()
        Status.textColor = UIColor.whiteColor()

    }

    func opened(endTime:String)
    {
        timeOfTheDay.text = String(getTime().hour)+" : "+String(getTime().minute)+" : "+String(getTime().second)
        timeOfTheDay.backgroundColor = UIColor.greenColor()
        timeOfTheDay.textColor = UIColor.whiteColor()

        Status.text = "Hi! still we are opened upto "+endTime
        Status.backgroundColor = UIColor.greenColor()
        Status.textColor = UIColor.whiteColor()
    }


    func uptoEvening()
    {

        let time = getTime().hour

        switch time
        {
            case 09...16: opened("17")  //set time for 09:00 to 16:59
            default:closed()
        }
    }

    func uptoNight()
    {

        let time = getTime().hour

        switch time
        {
        case 09...20: opened("21") //set time for 09:00 to 20:59
        default:closed()
        }

    }



    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }


}

Extension for swift 3:

extension Date {
    func dayOfWeek() -> Int? {
            let calender: Calendar = Calendar.current
            let component: DateComponents = (calender as NSCalendar).components(.weekday, from: self)
        return component.weekday
    }
}

enter image description here

Rajamohan S
  • 7,229
  • 5
  • 36
  • 54
  • `extension Date { func dayOfWeek() -> Int? { guard let calendar: Calendar = Calendar.current, let component: DateComponents = calendar.dateComponents(.day, from: self) else { return nil } return component.weekday } }` I'm having trouble with this part of the code in Xcode 8, do you know what has changed as it works perfect in Xcode 7 – Elfuthark Aug 06 '16 at 15:19
  • what was the error you got? It's work perfectly on Swift 2.0 – Rajamohan S Aug 07 '16 at 01:31
  • Yes it worked perfectly in Xcode 7 as I built and run it but then when the syntax changed in Swift 3 I let it auto fix everything itself but was left with one error `Type 'Set'has no member 'day'` – Elfuthark Aug 07 '16 at 01:39
  • `calendar.components([.Day], fromDate: date)` and fix this `return component.day` . Is it work? – Rajamohan S Aug 08 '16 at 01:24
  • Managed to get it working thank you but the time isn't being updated, it loads once so I will need to change the code slightly – Elfuthark Aug 08 '16 at 07:53