As I am new to swift, I stuck here from very longer,I am trying to get date from array between start date and end date,.
I have tried these code which refered from [https://stackoverflow.com/questions/10216991/fetching-dates-which-comes-between-startdate-and-enddate], But I am getting the error as "Value of type '[Date]' has no member 'filtered'".
In swift
var predicate = NSPredicate(format: "(SELF > %#) AND (SELF < %#)", startdate!, enddate!)
var result = arrayWithDates.filtered(using: predicate)
Expected output:
dateArray = ["12/01/1996","13/01/1996","15/01/1996","17/01/1996"]
startdate = "13/01/1996"
enddate = "17/01/1996"
output = ["13/01/1996","15/01/1996","17/01/1996"]
please help me to acheive this, thanks in advance
You can do it this way:
import Foundation
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT")
let dateArray = ["12/01/1996","13/01/1996","15/01/1996","17/01/1996"]
let startdate = dateFormatter.date(from: "13/01/1996")!
let enddate = dateFormatter.date(from: "17/01/1996")!
let output = dateArray
// Map your strings to Date objects excluding nils.
.compactMap { dateFormatter.date(from: $0) }
// Filter the mapped array by your condition.
.filter { $0 >= startdate && $0 <= enddate }
You can try below code, it works for me:
class ViewController: UIViewController {
var arrDates = NSArray()
var arrResultDates = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
arrDates = ["12/01/1996","13/01/1996","15/01/1996","17/01/1996"]
let strStartDate = "\(arrDates.firstObject!)"
let strEndDate = "\(arrDates.lastObject!)"
let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yyyy"
//Get start date
var startDate = formatter.date(from: strStartDate)
let endDate = formatter.date(from: strEndDate) // last date
// Formatter for printing the date, adjust it according to your needs:
let fmt = DateFormatter()
fmt.dateFormat = "dd/MM/yyyy"
let calendar = NSCalendar.current
while startDate! <= endDate! {
startDate = calendar.date(byAdding: .day, value: 1, to: startDate!)!
let strDate = fmt.string(from: startDate!)
if arrDates.contains(strDate)
{
arrResultDates.add(strDate)
}
}
print(arrResultDates)
}
Hope it will helps you :)
If you really need to use NSPredicate you have to bridge the array to NSArray which responds to filtered(using:
let predicate = NSPredicate(format: "(SELF > %#) AND (SELF < %#)", startdate!, enddate!)
let result = (arrayWithDates as NSArray).filtered(using: predicate)
However in the world of Swift this is not recommended. Just use the filter function with a closure
let result = arrayWithDates.filter { $0 >= startdate! && $0 <= enddate! }
Related
I have an array of dates like this :-
var dateArray = ["2016-04-20", "2016-04-22", "2016-04-25", "2016-04-30"]
and I want to find out the difference of days between them. I do some research and i am able to do that with only two dates here is the approach i did in finding the difference between two date
let dateFormatter = DateFormatter()
let isoDate = "2016-04-20"
let calendar = NSCalendar.current
let currentDate = Date()
And in my viewDidLoad method I did this
override func viewDidLoad() {
super.viewDidLoad()
// let components = calendar.dateComponents([.day], from: )
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.locale = Locale(identifier: "en_US_POSIX") //en_US_POSIX
let formatedStartDate = dateFormatter.date(from: isoDate)
let date = dateArray.compactMap { dateFormatter.date(from: $0) } // for date array
print(date)
let components = Set<Calendar.Component>([.day])
let differenceOfDate = Calendar.current.dateComponents(components, from: formatedStartDate!, to: currentDate )
print (differenceOfDate)
apiData()
}
As you can see in the code i created a constant let isoDate = "2016-04-20" and changed into formatedStartDate and find the difference between form this date to current date then it worked. But what if I have my own array of dates and how can i find the difference of my own array of dates and sort it into increasing or decreasing order. Please help?
Create the date formatter
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.locale = Locale(identifier: "en_US_POSIX") //en_US_POSIX
Map the date string array to Date instances
let dateStringArray = ["2016-04-20", "2016-04-22", "2016-04-25", "2016-04-30"]
let dateArray = dateStringArray.map {dateFormatter.date(from: $0)!} // add .sorted() if the array is unordered.
In a loop get the differences between adjacent items
var differences = [Int]()
for i in 0..<dateArray.count - 1 {
let dayComponent = Calendar.current.dateComponents([.day], from: dateArray[i], to: dateArray[i+1])
differences.append(dayComponent.day!)
}
print(differences)
I'm not sure if you only want to compare the dates next to each other or compare each and every date. In the first case go with vadians solution. In the other case you can do something like this:
let dateStrings = ["2016-04-20", "2016-04-22", "2016-04-25", "2016-04-30"]
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
let dates = dateStrings.compactMap { dateFormatter.date(from: $0) }
let calendar = Calendar.current
var differences: [Int] = []
for i in 0..<dates.count {
for j in i + 1..<dates.count {
if let difference = calendar.dateComponents([.day], from: dates[i], to: dates[j]).day {
differences.append(difference)
}
}
}
let descendingDifferences = differences.sorted(by: >)
print(descendingDifferences) // results in [10, 8, 5, 5, 3, 2]
I have Date() properties. startingAt and endingAt. And an array of Date(), which are alreadyRegistred. I have to create an array of strings with dates between startingAt and endingAt. StartingAt and endingAt are included and the last requirement is to exclude alreadyRegistred dates.
Do you have some elegant idea, how to do it? Thanks for help!
Edit: Maximum number of dates in final array will be about 7 days.
Dont forget that a Date is basically just a timestamp, and that you can have access to the addingTimeInterval(_:) method.
Knowing that, is very easy to do some calculation between two dates.
I do not have the whole knowledge about your required business logic, but here is a naive implementation that generates Dates between two dates. I'm sure you can run it in a playground and explore a little bit.
import UIKit
func intervalDates(from startingDate:Date, to endDate:Date, with interval:TimeInterval) -> [Date] {
guard interval > 0 else { return [] }
var dates:[Date] = []
var currentDate = startingDate
while currentDate <= endDate {
currentDate = currentDate.addingTimeInterval(interval)
dates.append(currentDate)
}
return dates
}
let startingDate = Date() // now
let endDate = Date(timeIntervalSinceNow: 3600 * 24 * 7) // one week from now
let intervalBetweenDates:TimeInterval = 3600 * 3// three hours
let dates:[Date] = intervalDates(from: startingDate, to: endDate, with: intervalBetweenDates)
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .long
let dateStrings = dates.map{dateFormatter.string(from: $0)}
print("NOW : \(startingDate)")
for (index, string) in dateStrings.enumerated() {
print("\(index) : \(string)")
}
print("END DATE : \(endDate)")
Try this and see:
// Start & End date string
let startingAt = "01/01/2018"
let endingAt = "08/03/2018"
// Sample date formatter
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
// start and end date object from string dates
var startDate = dateFormatter.date(from: startingAt) ?? Date()
let endDate = dateFormatter.date(from: endingAt) ?? Date()
// String date array, to be excluded
let alreadyRegistred = ["01/01/2018", "15/01/2018", "10/02/2018", "20/02/2018", "05/03/2018"]
// Actual operational logic
var dateRange: [String] = []
while startDate <= endDate {
let stringDate = dateFormatter.string(from: startDate)
startDate = Calendar.current.date(byAdding: .day, value: 1, to: startDate) ?? Date()
if (alreadyRegistred.contains(stringDate)) {
continue
} else {
dateRange.append(stringDate)
}
}
print("Resulting Array - \(dateRange)")
Here is result:
Resulting Array - ["02/01/2018", "03/01/2018", "04/01/2018", "05/01/2018", "06/01/2018", "07/01/2018", "08/01/2018", "09/01/2018", "10/01/2018", "11/01/2018", "12/01/2018", "13/01/2018", "14/01/2018", "16/01/2018", "17/01/2018", "18/01/2018", "19/01/2018", "20/01/2018", "21/01/2018", "22/01/2018", "23/01/2018", "24/01/2018", "25/01/2018", "26/01/2018", "27/01/2018", "28/01/2018", "29/01/2018", "30/01/2018", "31/01/2018", "01/02/2018", "02/02/2018", "03/02/2018", "04/02/2018", "05/02/2018", "06/02/2018", "07/02/2018", "08/02/2018", "09/02/2018", "11/02/2018", "12/02/2018", "13/02/2018", "14/02/2018", "15/02/2018", "16/02/2018", "17/02/2018", "18/02/2018", "19/02/2018", "21/02/2018", "22/02/2018", "23/02/2018", "24/02/2018", "25/02/2018", "26/02/2018", "27/02/2018", "28/02/2018", "01/03/2018", "02/03/2018", "03/03/2018", "04/03/2018", "06/03/2018", "07/03/2018", "08/03/2018"]
let startDate = Date()
let endDate = Date().addingTimeInterval(24*60*60*10) // i did this to get the end date for now
var stringdateArray = [String]()
if let days = getNumberofDays(date1: startDate, date2: endDate) {
for i in 0...days-1 {
let date = startDate.addingTimeInterval(Double(i)*24*3600)
let stringDate = getStringDate(fromDate: date, havingFormat: "yyyy-MM-dd")
if !(alreadyRegisteredArray.contains(stringDate)) { // checking if already registered
stringdateArray.append(stringDate)
}
}
}
and our helper method
let dateFormatter = DateFormatter()
func getStringDate(fromDate: Date,havingFormat: String) -> String {
dateFormatter.dateFormat = havingFormat
dateFormatter.amSymbol = "AM"
dateFormatter.pmSymbol = "PM"
let date = dateFormatter.string(from: fromDate)
return date
}
func getNumberofDays(date1: Date, date2: Date) -> Int? {
let calendar = NSCalendar.current
let date1 = calendar.startOfDay(for: date1)
let date2 = calendar.startOfDay(for: date2)
let components = calendar.dateComponents([.day], from: date1, to: date2)
return components.day
}
How Can I convert dates into string format?
I am getting dates between two dates ( From to end date). and I was using this below method
class Dates {
static func printDatesBetweenInterval(_ startDate: Date, _ endDate: Date) {
var startDate = startDate
let calendar = Calendar.current
let fmt = DateFormatter()
fmt.dateFormat = "yyyy-MM-dd"
while startDate <= endDate {
print(fmt.string(from: startDate))
startDate = calendar.date(byAdding: .day, value: 1, to: startDate)!
}
}
static func dateFromString(_ dateString: String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
return dateFormatter.date(from: dateString)!
}}
and printing dates here
Dates.printDatesBetweenInterval(Dates.dateFromString("2017-10-02"), Dates.dateFromString("2017-10-9"))
result:
2017-10-02 2017-10-03 2017-10-04 2017-10-05 2017-10-06 2017-10-07 2017-10-08 2017-10-09
Now, I want pass this dates to String format to calendar (I am using FSCalendar lib into app). I want this format
example:
["2017-10-02", "2017-10-03", "2017-10-04", "2017-10-05", "2017-10-06", "2017-10-07", "2017-10-08", "2017-10-09"]
Can anyone guide me . Thanks
static func getDatesStringBetweenInterval(_ startDate: Date, _ endDate: Date)-> String {
var retval = "["
var startDate = startDate
let calendar = Calendar.current
let fmt = DateFormatter()
fmt.dateFormat = "yyyy-MM-dd"
while startDate <= endDate {
if startDate < endDate {
retval.append("\"\(fmt.string(from: startDate))\", ")
} else {
retval.append("\"\(fmt.string(from: startDate))\"")
}
startDate = calendar.date(byAdding: .day, value: 1, to: startDate)!
}
retval.append("]")
return retval
}
Usage:
let aDate = Dates.dateFromString("2017-01-01")
let bDate = Dates.dateFromString("2017-01-05")
let datesString = Dates.getDatesStringBetweenInterval(aDate, bDate)
print(datesString)
Return date string array from your function, like this:
class Dates {
static func printDatesBetweenInterval(_ startDate: Date, _ endDate: Date) -> [String] {
var startDate = startDate
let calendar = Calendar.current
let fmt = DateFormatter()
fmt.dateFormat = "yyyy-MM-dd"
var arrayDateString = [String]()
while startDate <= endDate {
let stringDate = fmt.string(from: startDate)
print("stringDate - \(stringDate)")
arrayDateString.append(stringDate)
startDate = calendar.date(byAdding: .day, value: 1, to: startDate)!
}
return arrayDateString
}
static func dateFromString(_ dateString: String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
return dateFormatter.date(from: dateString)!
}}
Now, print string array
let dateStringArray = Dates.printDatesBetweenInterval(Dates.dateFromString("2017-10-02"), Dates.dateFromString("2017-10-9"))
print("dateStringArray - \(dateStringArray)")
Edit:
Now, as you said, you've data in following format:
let dataArray = [
{ "hlddate": "07-Aug", "frmdt": "07-Aug-2017", "todt": "07-Aug-2017" },
{ "hlddate": "15-Aug", "frmdt": "15-Aug-2017", "todt": "15-Aug-2017" },
{ "hlddate": "10-Oct", "frmdt": "10-Oct-2017","todt": "31-Oct-2017" }
]
Try with following solution (Copy-paste, dataAray and class structure and see result):
class Dates {
static func dateFromString(dateString: String, webResponseDateFormat: String = "yyyy-MM-dd") -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = webResponseDateFormat
return dateFormatter.date(from: dateString)
}}
static func printDatesBetweenInterval(startDate: Date, endDate: Date, requiredDateFormat: String = "yyyy-MM-dd") -> [String] {
var startDate = startDate
let calendar = Calendar.current
let fmt = DateFormatter()
fmt.dateFormat = requiredDateFormat
var arrayDateString = [String]()
while startDate <= endDate {
let stringDate = fmt.string(from: startDate)
print("stringDate - \(stringDate)")
arrayDateString.append(stringDate)
startDate = calendar.date(byAdding: .day, value: 1, to: startDate)!
}
return arrayDateString
}
if let stringDateArray = dataArray as? [[String : String]] {
for arrayElement in stringDateArray {
if let startDateString = arrayElement["frmdt"], let startDate = Dates.dateFromString(dateString: startDateString, webResponseDateFormat: "dd-MMM-yyyy"), let endDateString = arrayElement["todt"], let endDate = Dates.dateFromString(dateString: endDateString, webResponseDateFormat: "dd-MMM-yyyy") {
let dateStringArray = Dates.printDatesBetweenInterval(startDate: startDate, endDate: endDate, )
print("\n\n**\ndateStringArray - \(dateStringArray)")
}
}
}
Final Update
// Actual data structure
let dataArray = ["07-8-2017","08-08-2017","10-09-2017","20-10-2017",21-10-2017","23-10-2017", etc..]
Use above method and change data in server response section and getting result.
if let Streams = jsonDict["data"] as! [AnyObject]? {
for arrayElement in Streams {
if let startDateString = arrayElement["frmdt"] {
self.somedateFromString = self.convertStringToDate(string: startDateString as! String)
if let endDateString = arrayElement["todt"] {
self.somedateEndString = self.convertStringToDate(string: endDateString as! String)
}
self.presentdays = Dates.printDatesBetweenInterval(Dates.dateFromString(self.somedateFromString), Dates.dateFromString(self.somedateEndString))
self.totalHolidays.append(contentsOf: self.presentdays)
}
}
}
This is a very simple question, I am getting the day of a week using a snippet
if let weekday = getDayOfWeek("2017-05-01") {
print(weekday)
} else {
print("bad input")
}
func getDayOfWeek(_ today:String) -> Int? {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
guard let todayDate = formatter.date(from: today) else { return nil }
let myCalendar = Calendar(identifier: .gregorian)
let weekDay = myCalendar.component(.weekday, from: todayDate)
return weekDay
}
I need to create "weekday" as global variable can any one help.
What I did is:
class ViewController: UIViewController{
var weekday = NSInteger()
}
I am getting some error in snippet while declaring as above.
Declare currentWeekDay type to Int.
var weekday = Int()
Now access this instance property with if let.
if let weekday = getDayOfWeek("2017-05-01") {
//weekday has block scope and available only with in this if block
self.weekday = weekday
} else {
print("bad input")
}
Edit: Set timezone to get correct weekday
func getDayOfWeek(_ today:String) -> Int? {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
formatter.timeZone = TimeZone(abbreviation: "UTC")!
guard let todayDate = formatter.date(from: today) else { return nil }
let myCalendar = Calendar(identifier: .gregorian)
let weekDay = myCalendar.component(.weekday, from: todayDate)
return weekDay
}
I want to create a project where:
I create a task to do.
I create a stoping date for this task
I create three reminders using date picker
This I've done already. But now, I want the label to return for me the hour of the closest reminder (but I want the label to do this until the stoping date)
What I accomplish already is a playground project:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
let currentDate = dateFormatter.dateFromString("2016-01-03 16:30")
var firstDate = dateFormatter.dateFromString("2016-01-03 13:00")
var secondDate = dateFormatter.dateFromString("2016-01-03 16:00")
var thirdDate = dateFormatter.dateFromString("2016-01-03 17:00")
var datesArray = [firstDate, secondDate, thirdDate]
let firstInterval = firstDate?.timeIntervalSinceDate(currentDate!)
let secondInterval = secondDate?.timeIntervalSinceDate(currentDate!)
let thirdInterval = thirdDate?.timeIntervalSinceDate(currentDate!)
let intervalArray = [firstInterval, secondInterval, thirdInterval]
var aboveZeroIntervals = [NSTimeInterval]()
for interval in intervalArray {
if interval > 0 {
aboveZeroIntervals.append(interval!)
}
}
//print(aboveZeroIntervals)
for date in datesArray {
if date?.timeIntervalSinceDate(currentDate!) == aboveZeroIntervals.minElement() {
print(dateFormatter.stringFromDate(date!))
}
}
How to get only hours from first/second/thirdDate and display the hour from those three dates that is the closest to currentDate hour? And perfectly finish displaying it after currentDate exceeds stopDoingDate...
I came up with something like this (code below). Can anyone look at it and tell me if there is some way I can simplify this?
extension NSDate
{
func isGreaterThanDate(dateToCompare : NSDate) -> Bool
{
//Declare Variables
var isGreater = false
//Compare Values
if self.compare(dateToCompare) == NSComparisonResult.OrderedDescending
{
isGreater = true
}
//Return Result
return isGreater
}
}
let calendar = NSCalendar.currentCalendar()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
let currentDate = dateFormatter.dateFromString("2016-01-04 16:30")
let stopDoingDate = dateFormatter.dateFromString("2016-01-06 15:00")
if stopDoingDate!.isGreaterThanDate(currentDate!) {
let currentDateComponents = calendar.components([.Year, .Month, .Day, .Hour, .Minute], fromDate: currentDate!)
currentDateComponents.hour
currentDateComponents.minute
let dateTimeFormatter = NSDateFormatter()
dateTimeFormatter.dateFormat = "HH:mm"
let currentOnlyTime = dateTimeFormatter.dateFromString("\(currentDateComponents.hour):\(currentDateComponents.minute)")
var firstDate = dateFormatter.dateFromString("2016-01-03 13:00")
var firstDateComponents = calendar.components([.Hour, .Minute], fromDate: firstDate!)
var firstDateTime = dateTimeFormatter.dateFromString("\(firstDateComponents.hour):\(firstDateComponents.minute)")
var secondDate = dateFormatter.dateFromString("2016-01-03 16:00")
var secondDateComponents = calendar.components([.Hour, .Minute], fromDate: secondDate!)
var secondDateTime = dateTimeFormatter.dateFromString("\(secondDateComponents.hour):\(secondDateComponents.minute)")
var thirdDate = dateFormatter.dateFromString("2016-01-03 17:00")
var thirdDateComponents = calendar.components([.Hour, .Minute], fromDate: thirdDate!)
var thirdDateTime = dateTimeFormatter.dateFromString("\(thirdDateComponents.hour):\(thirdDateComponents.minute)")
var datesArray = [firstDateTime, secondDateTime, thirdDateTime]
let firstInterval = firstDateTime?.timeIntervalSinceDate(currentOnlyTime!)
let secondInterval = secondDateTime?.timeIntervalSinceDate(currentOnlyTime!)
let thirdInterval = thirdDateTime?.timeIntervalSinceDate(currentOnlyTime!)
let intervalArray = [firstInterval, secondInterval, thirdInterval]
var aboveZeroIntervals = [NSTimeInterval]()
for interval in intervalArray {
if interval > 0 {
aboveZeroIntervals.append(interval!)
}
}
//print(aboveZeroIntervals)
for date in datesArray {
if date?.timeIntervalSinceDate(currentOnlyTime!) == aboveZeroIntervals.minElement() {
let dateTime = calendar.components([.Hour, .Minute], fromDate: date!)
print(dateFormatter.dateFromString("\(currentDateComponents.year)-\(currentDateComponents.month)-\(currentDateComponents.day) \(dateTime.hour):\(dateTime.minute)")!)
print("Next todo hour is: \(dateTime.hour): \(dateTime.minute)")
}
}
} else {
print("Todo DONE")
}
I came up with this:
The only problem is to generate array of dates from currentDate to stopDoingDate but I will handle this alone (and probably post a solution here as well)
I used one of this forum post to generate extension to NSDate:
extension NSDate
{
func isGreaterThanDate(dateToCompare : NSDate) -> Bool
{
//Declare Variables
var isGreater = false
//Compare Values
if self.compare(dateToCompare) == NSComparisonResult.OrderedDescending
{
isGreater = true
}
//Return Result
return isGreater
}
}
//let calendar = NSCalendar.currentCalendar()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
let currentDate = dateFormatter.dateFromString("2016-01-04 14:00")!
let stopDoingDate = dateFormatter.dateFromString("2016-01-06 16:00")!
let date1 = dateFormatter.dateFromString("2016-01-04 09:00")
let date2 = dateFormatter.dateFromString("2016-01-04 15:00")
let date3 = dateFormatter.dateFromString("2016-01-05 09:00")
let date4 = dateFormatter.dateFromString("2016-01-05 15:00")
let date5 = dateFormatter.dateFromString("2016-01-06 09:00")
let date6 = dateFormatter.dateFromString("2016-01-06 15:00")
var dates: [NSDate] = [date1!, date2!, date3!, date6!, date5!, date4!]
if stopDoingDate.isGreaterThanDate(currentDate) {
var datesAfterCurrentDate = [NSDate]()
for date in dates {
if date.isGreaterThanDate(currentDate) {
datesAfterCurrentDate.append(date)
}
}
let sortedDatesAfterCurrentDate = datesAfterCurrentDate.sort({ $0.timeIntervalSince1970 < $1.timeIntervalSince1970 })
let dateToDisplay = sortedDatesAfterCurrentDate.first!
let timeFormatter = NSDateFormatter()
timeFormatter.timeStyle = .ShortStyle
print(timeFormatter.stringFromDate(dateToDisplay)) } else {
print("We are done here")
}
This forum is truly awesome! Thanks Leo Dabus for the simple idea!