简体   繁体   English

如何以秒为单位获取当前时间并在 Swift 中添加播放器持续时间?

[英]How to get current time in seconds and adds with player duration in Swift?

I want to get the current time in seconds and adds the seconds with player duration and other double value.我想以秒为单位获取当前时间,并将秒数与玩家持续时间和其他双精度值相加。

Heres the code I tried but didn't work.这是我试过但没有用的代码。

let date = Date()
        let calendar = Calendar.current
        let now = calendar.component(.second, from: date)
        let start_time = now + player.duration + (myModel.diff * 60)

The code for myModel.diff myModel.diff 的代码

var diff: Double = 1

and when I try this code, I'm getting error saying当我尝试这段代码时,我收到错误消息

Binary operator '+' cannot be applied to operands of type 'Int' and 'TimeInterval' (aka 'Double')二元运算符“+”不能应用于“Int”和“TimeInterval”(又名“Double”)类型的操作数

Please help me to solve this issue请帮我解决这个问题

You can get your current code to compile by casting now to a Double:您可以通过将now转换为 Double 来编译当前代码:

let start_time = Double(now) + player.duration + (myModel.diff * 60)

However, I'm not completely sure what you mean by "current time in seconds."但是,我不完全确定“当前时间以秒为单位”是什么意思。 For example, if the current time is 08:00:03, now will be 3 .例如,如果当前时间是 08:00:03,那么now将是3 At 08:01:15, it'll be 15 -- if that is your desired result, you're in good shape.在 08:01:15,将是15 - 如果这是您想要的结果,那么您的状态很好。

If your desired result is to get a more complete timestamp (seconds since 1970 is common, for example) of the current time, you can use something like:如果您想要的结果是获得当前时间的更完整的时间戳(例如,自 1970 年以来的秒数很常见),您可以使用以下内容:

Date().timeIntervalSince1970

Feel free to clarify your question and I can adjust the answer.随时澄清您的问题,我可以调整答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM