Skip to content

Instantly share code, notes, and snippets.

@joshdance
Created March 23, 2018 07:11
Show Gist options
  • Save joshdance/4d10ec39464bd5fd7a476899b4265f8e to your computer and use it in GitHub Desktop.
Save joshdance/4d10ec39464bd5fd7a476899b4265f8e to your computer and use it in GitHub Desktop.
See if a date is this week in Swift
func calcPushupsThisWeek(){
let calendar = Calendar.current
var thisWeeksPushups = 0
let today = Date()
let todayComponents = calendar.dateComponents([.weekOfYear], from: today)
//I have an array of workout objects with dateOfWorkout properties. You can use any date here
for workout in myArray {
let workoutComponents = calendar.dateComponents([.weekOfYear], from: workout.dateOfWorkout)
if todayComponents.weekOfYear == workoutComponents.weekOfYear
{
thisWeeksPushups = thisWeeksPushups + workout.numberOfPushups
}
} //end for workout
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment