2016-07-21 30 views
0

我有根據商店時間顯示的名爲open和close的圖像。根據印度標準時間,商店時間爲11點至11點,在此期間,我想在剩餘時間顯示開放圖像和封閉圖像。有人可以幫助我如何做到這一點。謝謝。Swift:如何根據當天的當前時間顯示兩個圖像中的一個

+0

http://stackoverflow.com/questions/ 29652771 /如何檢查時間是否在特定範圍內迅速檢查此鏈接 –

回答

0

首先,拿在小時當前時間(這是24小時格式)

let date = NSDate() 
let calendar = NSCalendar.currentCalendar() 
let components = calendar.components([.Hour], fromDate: date) 
let hour = components.hour 

然後檢查時間

if hour >= 11 && hour <= 22 { 
    // show open image 
} 
else { 
    // show close image 
} 
+0

如果它<= 23表示它將打開到23:59:59 – sanjeet

0
var component: NSDateComponents = NSCalendar.currentCalendar().components([.Hour, .Minute], fromDate: currentDate) 
var strStoreStatus: String = (component.hour() >= 11 && component.hour() <= 22) ? "OPEN" : "CLOSE" 
+0

「計時」 :[ [ 「11:30 AM」, 「11:30 PM」 ] ], 我有一個如上所述的JSON數據。你能幫我按照這個怎麼顯示。我知道如何下載數據,之後我需要幫助。比如,下載後如何將時間放在JSON中的component.hour()中。 – Devan

+0

var component:NSDateComponents = NSCalendar.currentCalendar()。components([。Hour,.Minute],fromDate:currentDate) – sanjeet

+0

現在你還可以分鐘,休息一下,你必須自己做 – sanjeet

相關問題