2017-12-30 56 views
0

を選択し、次の日に向けた時間を計算し、私はこのコードを持っている:アプリケーションは、それがイズムどの日見つけると、ユーザーがこれまで

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! 

をしかし、私ながら、それだけで次の日に一定の時間までの時間を算出し、それが検出したい。たとえば、今日が月曜日で、翌日が水曜日で、その日に選択された時間にカウントダウンを開始するとします。

ご協力いただければ幸いです。ありがとう。

更新 このコードを無視して、必要な機能をアプリに提供しようとしています。 このビューポートでは、コードの日時を確認し、別の日が 'true'で、それまでの時刻を表示したい場合は

+0

次回は[以前の質問](https://stackoverflow.com/questions/48029814/ios-count-down-until-next-selected-day-in-hours-minutes-seconds)を編集してください新しいものを掲示する。 – rmaddy

答えて

0

となります。Swift dateには、timeIntervalSinceというメソッドがあり、TimeInterval )の2つの日付の間。 DateComponentsFormatterを使用して、任意の単位を含む書式付き文字列に間隔を変換することができます。

+0

ジョシュ、これまでリンゴの開発者のウェブサイトからの助言は....あまりにも情報的であり、あなたが助けることができるどんなに私を助けてくれませんか?任意の良いチュートリアルですか?検索したものなど –

関連する問題