「SwiftUI」DateFormatter使用和时间倒计时

IOS 时间戳计算和显示

DateFormatter使用

clojure 复制代码
		let formatter = DateFormatter() //建立DateFormatter
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"  //设置Formatter显示格式
        formatter.timeZone = TimeZone(secondsFromGMT: 8)  //设置Formatter时区

时间戳

clojure 复制代码
func CountdownTime(buyTime:String = "yyyy-MM-dd HH:mm:ss"){
		let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        formatter.timeZone = TimeZone(secondsFromGMT: 8)
        let buyDate = formatter.date(from: buyTime) //先将String转成Date类型
        let buyStamp:TimeInterval = buyDate!.timeIntervalSince1970//将Date转成时间戳
        }

通过时间戳设置一个15分钟的倒计时

输入一个String类型的时间格式,对于输入时间进行一个15分钟的倒计时,如果现在时间超过15分钟,则进行一系列需求操作,如TimeOut设为True

clojure 复制代码
func CountdownTime(buyTime:String = "yyyy-MM-dd HH:mm:ss"){
        let formatter = DateFormatter()//对于输入的时间进行格式和时间戳的转换
        let countdownFormatter = DateFormatter()//倒计时的时间格式和时间戳转换
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        formatter.timeZone = TimeZone(secondsFromGMT: 8)
        countdownFormatter.dateFormat = "mm:ss"
        countdownFormatter.timeZone = TimeZone(secondsFromGMT: 8)
        let buyDate = formatter.date(from: buyTime)
   //将输入的String转换成时间格式总会多8个小时,因为时区的原因吧,不知道为什么加了时区仍然会出问题
        let buyStamp:TimeInterval = buyDate!.timeIntervalSince1970
        let now = Date() //获取一个现在的时间
        let nowStamp:TimeInterval = now.timeIntervalSince1970
        let countdownStamp:TimeInterval = buyStamp - 28800 + 900 - nowStamp
        //输入时间-多出的8小时的时间戳+15分钟倒计时时间戳-现在的时间戳 = 倒计时时间戳
        let countdownDate:Date = Date(timeIntervalSince1970: countdownStamp)
        let countdownString:String = countdownFormatter.string(from: countdownDate)
        //将倒计时用String的形式展示
        self.countdownTime = countdownString
        if(countdownStamp <= 0){
            self.TimeOut = true  //15分钟倒计时结束后,TimeOut变为True
            self.countdownTime = "00:00"
            self.timer.upstream.connect().cancel()
        }
    }

页面实时展示,则需要使用.onReceive(timer)进行实时轮询

部分相关代码如下

clojure 复制代码
 @State var countdownTime = "00:00"
    private let timer = Timer.publish(every: 1, on: .current, in: .common).autoconnect()
    var body: some View {
        VStack{
            HStack{
                      Group{
                               Text("剩余时间:")
                               Text(countdownTime)
                           }
                           .onReceive(timer) { value in
                               CountdownTime(buyTime: "2022-04-20 13:30:30")
                           }
                        }
        }
    }

    func CountdownTime(buyTime:String = "yyyy-MM-dd HH:mm:ss"){
            let formatter = DateFormatter()
            let countdownFormatter = DateFormatter()
            formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
            formatter.timeZone = TimeZone(secondsFromGMT: 8)
            countdownFormatter.dateFormat = "mm:ss"
            countdownFormatter.timeZone = TimeZone(secondsFromGMT: 8)
            let buyDate = formatter.date(from: buyTime)
            let buyStamp:TimeInterval = buyDate!.timeIntervalSince1970
            let now = Date()
            let nowStamp:TimeInterval = now.timeIntervalSince1970
            let countdownStamp:TimeInterval = buyStamp - 28800 + 900 - nowStamp
            let countdownDate:Date = Date(timeIntervalSince1970: countdownStamp)
            let countdownString:String = countdownFormatter.string(from: countdownDate)
            self.countdownTime = countdownString
            if(countdownStamp <= 0){
                self.countdownTime = "00:00" //如果倒计时结束,倒计时一直显示00:00
                self.timer.upstream.connect().cancel()
            }
        }

页面显示效果

  • 可以自己通过调整DateFormatter中的显示格式展示自己所需求的格式
  • 时间戳也可以获取从现在的时间到输入的时间的时间戳,具体可访问Apple官方开发文档

developer.apple.com/documentati...

相关推荐
zhyongrui14 小时前
SnipTrip 发热优化实战:从 60Hz 到 30Hz 的性能之旅
ios·swiftui·swift
大熊猫侯佩1 天前
赛博深渊(上):用 Apple Foundation Models 提炼“禁忌知识”的求生指南
llm·swiftui·大语言模型·foundationmodel·apple ai·apple 人工智能·summarize
zhyongrui3 天前
SwiftUI 光晕动画性能优化:消除托盘缩放卡顿的实战方案
ios·性能优化·swiftui
大熊猫侯佩4 天前
星际穿越:SwiftUI 如何让 ForEach 遍历异构数据(Heterogeneous)集合
swiftui·swift·遍历·foreach·any·异构集合·heterogeneous
符哥20084 天前
对比ArkTsUI和Flutter和 SwiftUI 和Jetpack Compose四个框架语法及使用场景。
flutter·ios·swiftui
大熊猫侯佩5 天前
越狱沙盒:SwiftUI fileImporter 的“数据偷渡”指南
swiftui·url·沙箱·sandbox·readfile·file importer·uniformtype
大熊猫侯佩9 天前
拯救巴别塔:WWDC24 全新 Translation API 实战
swiftui·wwdc·language·coreml·translation api·翻译接口·translationsess
初级代码游戏10 天前
iOS开发 SwiftUI 8:NavigationView 导航
ios·swiftui·swift
QWQ___qwq13 天前
1-s2.0-S0031320324008811-讲解
swiftui
Swift社区16 天前
使用 MetricKit 监控应用性能
ios·swiftui·swift