gpt4 book ai didi

ios - 应用程序找出当前是哪一天并计算用户选择的第二天的时间

转载 作者:行者123 更新时间:2023-11-30 11:54:01 27 4
gpt4 key购买 nike

到目前为止,我有这段代码:

var seconds = 0
var minutes = 0
var hours = 0


var secondsUntilMidnight = 0
var minutesUntilMidnight = 0
var hoursUntilMidnigh = 0
var day = 0
var month = 0
var year = 0
//xvar day = [ hours, minutes, seconds]

var damian = "DayofWeek"

var monday = true
var tuesday = false
var wednesday = true
var thursday = false
var friday = false
var saturday = true
var sunday = false
var currentDay = 0
var nextworkingday = 0
var dayOfTheWeek = [Bool]()///Var collecting bools for each day
var dayOfTheWeekText = [String]()///Collecing name for each day
where the weekday Bool values are adjustable. I want the app to detect the current time and date, and calculate the time until the next day that is set as true.

This is what I have tried so far:


let now = Date()
let calendar = Calendar.current
let components = DateComponents(calendar: calendar, hour:11 , minute : 28 ) // <- 17:00 = 5pm
// let components2 = DateComponents(calendar: calendar day : 1, _)
let next5pm = calendar.nextDate(after: now, matching: components, matchingPolicy: .nextTime)!
print (now )
let next = calendar.nextDate(after: now, matching: components, matchingPolicy: .nextTime)!


let diff = calendar.dateComponents([.hour, .minute, .second ], from: now, to: next5pm)

///Second time checker
let calendar2 = Calendar.current
let components2 = DateComponents(calendar: calendar2, hour:00 , minute : 00 ) // <- 17:00 = 5pm
let next1pm = calendar2.nextDate(after: now, matching: components2, matchingPolicy: .nextTime)!

let next2 = calendar2.nextDate(after: now, matching: components2, matchingPolicy: .nextTime)!


let diff2 = calendar2.dateComponents([.hour, .minute, .second ], from: now, to: next1pm)
secondsUntilMidnight = diff2.second!
minutesUntilMidnight = diff2.minute!
hoursUntilMidnigh = diff2.hour!

hours = diff.hour!
minutes = diff.minute!
seconds = diff.second!

但它只计算直到下一天某个时间的时间,而我希望它检测到。例如,如果今天是星期一,第二天是星期三,则开始倒计时到当天选定的时间。

如有任何帮助,我们将不胜感激。谢谢。

更新忽略代码,它只是我为尝试为应用程序提供所需功能所做的工作的一个示例!在此视口(viewport)中,我希望代码检查今天是哪一天,查看另一天何时为“真”并计算到那时的时间

最佳答案

Swift date 有一个方法 timeIntervalSince它将计算两个日期之间的时间间隔(即秒数)。然后您可以使用 DateComponentsFormatter将间隔转换为包含您想要的任何单位的格式化字符串。

关于ios - 应用程序找出当前是哪一天并计算用户选择的第二天的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48034078/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com