Swift:DatePicker-Timer?

问题描述 投票:2回答:2

嘿!

这是我的问题:

所以我有一个日期选择器,它主要用于在达到设定的时间后发送消息。但是,我希望看到时间用完(类似于计时器),直到到达时间为止。最好采用(hh:mm:ss)格式,因为日期选择器是以计时器格式设置的。

我只是编程的初学者。所以我不知道该怎么办:/

如果有人可以帮助我,我将非常高兴!

非常感谢。

     let date = NSDate()
     let calendar = Calendar.current

     let components = calendar.dateComponents([.hour, .minute, .month, .year, .day], from: 
     date as Date)

     let currentDate = calendar.date(from: components)

     let userCalendar = Calendar.current

     let competitionDate = self.datepicker.isSelected

     let competition = userCalendar.date(from: competitionDate as DateComponents)!

     let CompetitionDifference = calendar.dateComponents([.hour, .minute, .second], from: 
     currentDate!, to: competition)


     let hoursLeft = CompetitionDifference.hour
     let minutesLeft = CompetitionDifference.minute
     let secondsLeft = CompetitionDifference.second

     print("hours:", hoursLeft ?? "N/A", "minutes:", minutesLeft ?? "N/A", "seconds:", 
     secondsLeft ?? "N/A")

    countDownLabel.text = "\(daysLeft ?? 0) hours, \(hoursLeft ?? 0) minutes, \(minutesLeft 
    ?? 0) seconds"
swift xcode timer datepicker countdown
2个回答
0
投票

首先,你只是不能写:

let competition = userCalendar.date(from: competitionDate as DateComponents)!

如果competitionDate不是DateComponents而是说Date,它只会触发错误!

然后,如果您只想要时间DateComponentsFormatter的字符串表示形式适合您

尝试:

let date = Date()
let calendar = Calendar.current
let userCalendar = Calendar.current

let competitionDate = date + 4899
let comps = userCalendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: competitionDate)



let formatter = DateComponentsFormatter()
formatter.unitsStyle = .full
formatter.string(from: date, to: competitionDate) // "1 hour, 21 minutes, 39 seconds"
formatter.unitsStyle = .short
formatter.string(from: date, to: competitionDate) // "1 hr, 21 min, 39"secs"
formatter.unitsStyle = .brief
formatter.string(from: date, to: competitionDate) // "1hr 21min 39secs"
formatter.unitsStyle = .abbreviated
formatter.string(from: date, to: competitionDate) // "1h 21m 39s"
formatter.unitsStyle = .positional
formatter.string(from: date, to: competitionDate) // "1:21:39"
formatter.includesTimeRemainingPhrase = true
formatter.string(from: date, to: competitionDate) // "1:21:39 remaining"


0
投票

欢迎您加入。这样的事情怎么样:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head><meta http-equiv="content-type" content="text/html; charset=utf-8">
    <title>Timer Demo</title>
  </head>
  <body>
    <div style="background-color: yellow;">A Minunte passing by as a percentage.</div>
    <div id='percentage' style="background-color: lime;">Timer percentage!</div>
  </body>

<script>
// End in 2h.
let end = Date.now() + 7200 * 1000;

function pad(s)
{
  while (s.length < 2) { s = '0' + s; }
  return s;
}

/** setInterval allows to define a function like this anonymous one.
 * This will be called after each interval defined in milli seconds as
 * the second parameter. (Here 1000ms)
 * The second paramter is the number of milliseconds that passes between */
window.setInterval(function()
{
  let div = document.getElementById('percentage');
  let now = Date.now();
  let diff = (end - now) / 1000.;
  let hours = Math.trunc(diff / 3600);
  diff = diff - (hours * 3600);
  let minutes = Math.trunc(diff / 60);
  diff = diff - (minutes * 60);
  let seconds = Math.trunc(diff);
  div.innerHTML = pad(String(hours)) + ':' + pad(String(minutes)) + ':' + pad(String(seconds));
}, 1000.);

</script>
</html>

此示例已准备就绪。只需将其粘贴到文件中,然后将其加载到浏览器中即可。

© www.soinside.com 2019 - 2024. All rights reserved.