Iterate through the year to every NSDate every day in Swift

Hello, I have a method that returns an array of times every day.

prayTimesDate(date: NSDate, latitide : Double, longitude : Double, timeZone : Double) -> NSMutableArray

I need to go through a whole year, or maybe a date range, to get an array of times every day for a whole year. I found a lot of links in ruby ​​and python on how to do this, but I could not find anything for swift or objective-c. Are there any built-in methods in quick that this will accomplish? If not, someone can help me as I am still new to programming. Any input is welcome.

This is objective-c code for a method that I am linking to my quick project

- (NSMutableArray *)prayerTimesDate:(NSDate *)date latitude:(double)latitude longitude:(double)longitude andTimezone:(double)timezone
{
    unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:unitFlags fromDate:date];

    NSInteger year = [components year];
    NSInteger month = [components month];
    NSInteger day = [components day];

    return [self getDatePrayerTimesForYear:year month:month day:day latitude:latitude longitude:longitude andtimeZone:timezone];
}
+4
source share
4

, prayerTimesDate: , , prayerTimesDate:, , , :

func yearlyPrayerDatesFromCurrentDate (latitude:Double, longitude:Double, timezone:Double) -> NSMutableArray {

    // Set "date" to equal the current day
    var date:NSDate! = NSDate()

    // Increment "date" by one year to calculate the ending
    // date for the loop
    let gregorian:NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
    let dateComponents = NSDateComponents()
    dateComponents.year = 1
    let endingDate:NSDate! = gregorian.dateByAddingComponents(dateComponents, toDate: date, options: nil)

    // Create an array to hold *all* the returned 
    // results for the year
    var datesArray = NSMutableArray()

    // Loop through each date until the ending date is
    // reached
    while date.compare(endingDate) != NSComparisonResult.OrderedDescending {
        // Call your prayerTimesDate: method on the current
        // date to get that date prayer times and add the
        // times from the returned array to the datesArray
        datesArray.addObjectsFromArray(prayerTimesDate(date, latitude: latitude, longitude: longitude, andTimezone: timezone))

        // increment the date by 1 day
        let dateComponents = NSDateComponents()
        dateComponents.day = 1
        date = gregorian.dateByAddingComponents(dateComponents, toDate: date, options: nil)
    }

    return datesArray
}
+5

14 ( NSCalendar):

let ti:NSTimeInterval = 24*60*60 //one day
let dateFrom = NSDate() //Now
let dateTo = dateFrom.dateByAddingTimeInterval(24*60*60*14) //14 Days later

var nextDate = NSDate()
var endDate = dateTo.dateByAddingTimeInterval(ti)

while nextDate.compare(endDate) == NSComparisonResult.OrderedAscending
{      
    print("nextDate:", nextDate)
    nextDate = nextDate.dateByAddingTimeInterval(ti)
}
+2

NSDateComponents 1 NSDate . , ( ), dateByAddingComponents:toDate:options: , .

0

From an Apple document: To calculate the sequence of dates, use the enumerateDatesStartingAfterDate: matchingComponents: options: usingBlock: method instead of calling this method (- nextDateAfterDate: matchingComponents: options :) in the loop with the previous loop iteration result.

As I understand it, it will iterate over all dates that match "matchingComponents", until you finish the iteration of "stop.memory = true"

//: Playground - noun: a place where people can play

import UIKit

let calendar = NSCalendar.currentCalendar()
let startDate = calendar.startOfDayForDate(NSDate())
let finishDate = calendar.dateByAddingUnit(.Day, value: 10, toDate: startDate, options: [])
let dayComponent = NSDateComponents()
dayComponent.hour = 1

calendar.enumerateDatesStartingAfterDate(startDate, matchingComponents: dayComponent, options: [.MatchStrictly]) { (date, exactMatch, stop) in
    print(date)
    if date!.compare(finishDate!) == NSComparisonResult.OrderedDescending {
        // .memory gets at the value of an UnsafeMutablePointer
        stop.memory = true
    }
}
0
source

All Articles