我有开始日期时间和结束日期时间 如何在 iOS Swift 中显示结束销售 4

I have Start Date Time And End Date Time How To Show End Sale in iOS Swift 4

我有开始日期和结束日期字符串,如何像 this 图片一样显示结束促销计时器?

"Start-date":"Dec 18, 2019 05:15:00 +0000","End-date":"Dec 27, 2019 11:15:39 +0000"

请帮忙!

您应该先将日期字符串转换为日期,然后您可以使用计时器根据开始和结束日期更新计时器标签。

使用CalendardateComponents方法找出日期之间的日、时、分、秒的差异,然后像你附上的图片一样在日、时、分、秒标签上设置值.

示例实现:

var timer:Timer?
var endDate:Date?

override func viewWillAppear(_ animated: Bool) {

    super.viewWillAppear(animated)

    startTimer()
}

override func viewWillDisappear(_ animated: Bool) {

    super.viewWillDisappear(animated)

    stopTimer()
}

func startTimer(){

    let endDateStr = "Dec 27, 2019 11:15:39 +0000"

    let dateFormat = "MMM d, yyyy HH:mm:ss Z"
    let dateFormater = DateFormatter()
    dateFormater.dateFormat = dateFormat

    endDate = dateFormater.date(from: endDateStr)
    //stop timer if it's already running
    stopTimer()
    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateSaleTime), userInfo: nil, repeats: true)
}

func stopTimer(){

    if timer != nil{

        timer!.invalidate()
        timer = nil
    }
}

func updateSaleTime(){

    guard let d2 = endDate else {
        stopTimer()//Check if the date-format is correct for end date string.
        return
    }

    let cal = Calendar.current

    let components = cal.dateComponents([.day, .hour, .minute, .second], from: Date(), to: d2)
    let day = components.day!
    let hour = components.hour!
    let minute = components.minute!
    let second = components.second!

    //set the value on day, hour, minute and second labels as like you attached image.
}

如果您计算开始日期和结束日期之间的差异,它将始终相同。相反,您可以计算从当前日期到 endDate 的差异。 检查以下实现。

  class ViewController: UIViewController {

        let endDate : Date? = {
            // To create Date from date string received from server 
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "MMM dd, yyyy HH:mm:ss Z"
            // Convert to desired Timezone
            return dateFormatter.date(from: "Dec 18, 2019 06:30:39 +0000")
        }()

        var timer : Timer?

        private func starCountDown() {
           timer =  Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
        }

        private func stopCountDown() {
            timer?.invalidate()
        }

        @objc func updateTime() {

             guard let endDate = endDate else {
                stopCountDown()
                return
            }

            let countdown = Calendar.current.dateComponents([.day, .hour, .minute, .second], from: Date(), to: endDate)
            let days = countdown.day!
            let hours = countdown.hour!
            let minutes = countdown.minute!
            let seconds = countdown.second!

            if days <= 0 && hours <= 0 && minutes <= 0 && seconds <= 0  {
                stopCountDown()
                print("Offer Expired")
                return
            }

            print(String(format: "%02d Days , %02d Hours, %02d Mins, %02d Sec", days, hours, minutes, seconds))
            // set values as per your requirement
        }

        override func viewWillDisappear(_ animated: Bool) {
            super.viewWillAppear(animated)
            stopCountDown()
        }


        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
           starCountDown()
        }

    }