Convert date "HH" to Int?

Hi!

I want to fetch current date in format "HH" and convert it to Int so that i can use it in calculations. I have managed to set the current date to update regularly, and format it to "HH" in a string, but now I am stuck... I need the number of hours between 06:00 -> now, but if now<10 i need number of hours between 06:00 yesterday -> now.

Any help is much appreciated!

import SwiftUI
import Foundation

struct ContentView: View {

    @State var date = Date()


    @State var vekt = Int(70)
    @State var temp = Double(37.0)
    @State var infusjon = Int(0)
    @State var medVaske = Int(0)
    @State var sag = Int(0)
    @State var blodning = Int(0)
    @State var urin = Int(0)

    var sagIn: Int{sag*250}
    var totIn: Int{infusjon+medVaske+(sag*250)}
    var totUt: Int{urin+blodning}
    var totUtKorr: Int{urin+(blodning*3)}

    var body: some View {
        ScrollView{
            VStack{
                Text("Væskebalanse") .font(.title)
                Text(timeString(date: date)).onAppear(perform: {let _ = self.updateTimer})
            }
        }
    }
    var timeFormat: DateFormatter {
        let formatter = DateFormatter()
        formatter.dateFormat = "HH"
        return formatter
    }
    func timeString(date: Date) -> String {
         let time = timeFormat.string(from: date)
         return time
    }
    var updateTimer: Timer {
         Timer.scheduledTimer(withTimeInterval: 1, repeats: true,
                              block: {_ in
                                 self.date = Date()
                               })
    }
    //----HOW TO??-----let klokke = Int(timeString(date: String))
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
Answered by OOPer in 695415022

Why don't you do it as you describe?

struct ContentView: View {
    
    @State var date: Date = Date()

    //@State...
    
    //var...
    
    let timer = Timer.publish(every: 1, on: .main, in: .common)
        .autoconnect()
    
    var body: some View {
        ScrollView{
            VStack{
                Text("Væskebalanse")
                    .font(.title)
                Text(timeString(date: date))
            }
        }
        .onReceive(timer) {_ in
            self.date = Date()
            let hh = timeString(date: self.date)
            let now = Int(hh)!
            let hoursSince0600: Int
            if now < 10 {
                //number of hours between 06:00 yesterday -> now
                hoursSince0600 = now + (24-6)
            } else {
                //number of hours between 06:00 -> now
                hoursSince0600 = now-6
            }
            print(hoursSince0600)
            //...
        }
    }
    
    let timeFormat: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "HH"
        formatter.locale = Locale(identifier: "en_US_POSIX")
        return formatter
    }()
    
    func timeString(date: Date) -> String {
        let time = timeFormat.string(from: date)
        return time
    }
}
Accepted Answer

Why don't you do it as you describe?

struct ContentView: View {
    
    @State var date: Date = Date()

    //@State...
    
    //var...
    
    let timer = Timer.publish(every: 1, on: .main, in: .common)
        .autoconnect()
    
    var body: some View {
        ScrollView{
            VStack{
                Text("Væskebalanse")
                    .font(.title)
                Text(timeString(date: date))
            }
        }
        .onReceive(timer) {_ in
            self.date = Date()
            let hh = timeString(date: self.date)
            let now = Int(hh)!
            let hoursSince0600: Int
            if now < 10 {
                //number of hours between 06:00 yesterday -> now
                hoursSince0600 = now + (24-6)
            } else {
                //number of hours between 06:00 -> now
                hoursSince0600 = now-6
            }
            print(hoursSince0600)
            //...
        }
    }
    
    let timeFormat: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "HH"
        formatter.locale = Locale(identifier: "en_US_POSIX")
        return formatter
    }()
    
    func timeString(date: Date) -> String {
        let time = timeFormat.string(from: date)
        return time
    }
}

@OOPer

Thank you so much! Seems so easy when a pro explains it... I am a beginner with no coding background what so ever, so I am very grateful for all the helpful people in this forums!

Convert date "HH" to Int?
 
 
Q