使用当前时间提前 1.5 小时计算时间并将其显示在列表中 [英] Work out time 1.5 hours ahead using current time and display this in the list

查看:24
本文介绍了使用当前时间提前 1.5 小时计算时间并将其显示在列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,有人在桌子上按下它,它显示当前时间,即客户的到达时间.

I have the following code someone presses on the Table and its displays the current time which is the arrival time of a customer.

我想显示他们必须离开的时间,在当前时间旁边这将始终提前 1.5 小时,我不知道如何做到这一点.我尝试过的所有操作都出现错误.

I want to display the time they must leave by, next to the current time this will always be 1.5 hours ahead I can not work out how to do this. everything I have tried comes back with an error.

仍然是 Xcode 的新手

Still new to Xcode

任何帮助都会很棒

    import SwiftUI

struct TimeListView: View {
    
    @State var tableOne = false
    @State var tableTwo = false
    @State var tableThree = false
    
    
    var body: some View {
        // Title
        
        VStack {
            Text("Arrival Times")
                .font(.title)
                .fontWeight(.bold)
            
            // List View
            List {
                
                // Table 1
                HStack {
                    
                    Button(action: {
                        self.tableOne.toggle()
                    }, label: {
                        Text("Table 1 -")
                    })
                    if tableOne {
                        Text(getCurrentTime())
                    }
                }
                
                // Table 2
                HStack {
                    
                    Button(action: {
                        self.tableTwo.toggle()
                    }, label: {
                        Text("Table 2 -")
                    })
                    if tableTwo {
                        Text(getCurrentTime())
                    }
                }
                // Table 3
                HStack {
                    
                    Button(action: {
                        self.tableThree.toggle()
                    }, label: {
                        Text("Table 3 -")
                    })
                    if tableThree {
                        Text(getCurrentTime())
                    }
                }
                
            }
            
        }
        
    }
}

struct TimeListView_Previews: PreviewProvider {
    static var previews: some View {
        TimeListView()
    }
}

// Get Current Time Function

func getCurrentTime() -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale(identifier: "en_UK_POSIX")
    dateFormatter.dateFormat = "HH:mm"
    
    return dateFormatter.string(from: Date())

推荐答案

你需要创建一个日期并添加 1.5 小时,你也忘记为他们创建 3 个不同的状态.

you need make a date and add 1.5 hour to it, also you forgot create 3 deferent State for them.

import SwiftUI

struct ContentView: View {
    var body: some View {
        
        TimeListView()
    }
}


struct TimeListView: View {
    
    @State private var table1: Bool = false
    @State private var table2: Bool = false
    @State private var table3: Bool = false

    @State private var timeForShow1: String?
    @State private var timeForShow2: String?
    @State private var timeForShow3: String?
    
    var body: some View {
        
        
        VStack {
            
            Text("Arrival Times")
                .font(.title)
                .fontWeight(.bold)
            
            List {
                
                HStack {
                    
                    Text("Table 1 -")
                        .onTapGesture {
                            
                            if table1 { table1.toggle() }
                            else { timeForShow1 = getCurrentTime; table1.toggle() }
                            
                        }
                    
                    if table1 { Text(timeForShow1 ?? "not available!") }
                    
                }
                
                
                HStack {
                    
                    Text("Table 2 -")
                        .onTapGesture {
                            
                            if table2 { table2.toggle() }
                            else { timeForShow2 = getCurrentTime; table2.toggle() }
                            
                        }
                    
                    if table2 { Text(timeForShow2 ?? "not available!") }
                    
                }
                
                
                
                HStack {
                    
                    Text("Table 3 -")
                        .onTapGesture {
                            
                            if table3 { table3.toggle() }
                            else { timeForShow3 = getCurrentTime; table3.toggle() }
                            
                        }
                    
                    if table3 { Text(timeForShow3 ?? "not available!") }
                    
                }
                
                
                
                
            }
            
        }
        
        
    }
}


var getCurrentTime: String? {
    
    if let date = Calendar.current.date(byAdding: .minute, value: 90, to: Date()) {
        
        let dateFormatter = DateFormatter()
        dateFormatter.locale = Locale(identifier: "en_UK_POSIX")
        dateFormatter.dateFormat = "HH:mm"
        return dateFormatter.string(from: date)
        
    }
    else {
        
        return nil
    }
    
}

这篇关于使用当前时间提前 1.5 小时计算时间并将其显示在列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆