Grouping Array of Objects by Custom Time Intervals - ios

I have an array of posts which I currently group by day. I use the below functions to do so;
private func splitDay(from date: Date) -> Date {
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day], from: date)
return calendar.date(from: components)!
}
private func sectionPosts(posts: [Post]) {
let groups = Dictionary(grouping: posts) { (posts) in
return splitDay(from: Date(timeIntervalSince1970: posts.createdOn))
}
self.sections = groups.map { (date, posts) in
return PostSection(date: date, posts: posts)
}
}
However, i'd like to implement a custom grouping which would be the following;
Today
Yesterday
This Week
This Month
Older
How would I build this into my grouping function? My section struct is like such;
struct PostSection {
var date: Date
var posts: [Post]
}

Since you are trying to group your data into groups such as "today", "yesterday", "this week" and "this month", you should first create a type to represent these groups:
enum PostGroup {
case today
case yesterday
case thisWeek
case thisMonth
case older
case coming // added "coming" group so that the groups cover all possible dates
}
Then your PostSection struct would have a PostGroup property, rather than a Date property:
struct PostSection {
let group: PostGroup
let posts: [Post]
}
Now we just need a function that goes (Post) -> PostGroup that we can pass to Dictionary(grouping:by:). This can be implemented just by comparing the date components of the post date with that of today:
func group(for post: Post) -> PostGroup {
let today = Date()
let calendar = Calendar.current
let postDateComponents = calendar.dateComponents([.year, .month, .day], from: post.createdOn)
let todayDateComponents = calendar.dateComponents([.year, .month, .day], from: today)
if postDateComponents == todayDateComponents {
return .today
}
let daysDifference = calendar.dateComponents([.day], from: postDateComponents, to: todayDateComponents)
if daysDifference.day == 1 {
return .yesterday
}
let postWeekComponents = calendar.dateComponents([.weekOfYear, .yearForWeekOfYear], from: post.createdOn)
let todayWeekComponents = calendar.dateComponents([.weekOfYear, .yearForWeekOfYear], from: today)
if postWeekComponents == todayWeekComponents {
return .thisWeek
}
if postDateComponents.year == todayDateComponents.year &&
postDateComponents.month == todayDateComponents.month {
return .thisMonth
}
if post.createdOn < today {
return .older
} else {
return .coming
}
}
To finish it off:
private func sectionPosts(posts: [Post]) {
let groups = Dictionary(grouping: posts, by: group(for:))
self.sections = groups.map { (group, posts) in
return PostSection(group: group, posts: posts)
}
}

This is not an elegant solution, but it solves the problem, please feel free to optimize this function, specially since date calculations can be slow.
private func preSplitProcess(from date: Date) -> Date {
let calendar = Calendar.current
let isToday = calendar.isDateInToday(date)
if isToday {
return date
} else {
let wasYesterday = calendar.isDateInYesterday(date)
if wasYesterday {
return date
} else {
let bowComponents = calendar.dateComponents([.weekOfYear], from: date)
let beginningOfWeek = calendar.date(from: bowComponents)! // Handle errors, please
if beginningOfWeek < date {
return date
} else {
let bomComponents = calendar.dateComponents([.month], from: date)
let beginningsOfMonth = calendar.date(from: bomComponents)! // Handle errors, please
if beginningsOfMonth < date {
return date
} else {
var components = DateComponents()
components.year = 1970
components.month = 01
components.day = 01
let oldDate = calendar.date(from: components)!
return oldDate
}
}
}
}
}
Add this before you call your splitDay function, since this doesn't reduce the return dates to the same date.

Related

how counting down the days in swift?

I am new to Swift and I have not worked with NSDate. For my app I need how to make to calculate how many days there are until the event. The date of the event is written with DatePicker on Firebase, and I need to calculate from the current date how many days are left until the day it's written. All I need is to count down the days.
Convert your data from DatePicker to Date object, and you can use the following function which returns an Int, a representation of the number of days to a passed date.
func daysTo(date: Date) -> Int? {
let calendar = Calendar.current
let date1 = calendar.startOfDay(for: Date())
let date2 = calendar.startOfDay(for: date)
let components = calendar.dateComponents([.day], from: date1, to: date2)
return components.day
}
you can use this extension for find difference between date:
extension Date {
public func diffrenceTime() -> (Int, Int) {
var cal = Calendar.init(identifier: .persian)
let d1 = Date()
let components = cal.dateComponents([.hour, .minute], from: self, to: d1)
let diffHour = components.hour!
let diffMinute = components.minute!
return (diffHour, diffMinute)
}
public func fullDistance(from date: Date, resultIn component: Calendar.Component, calendar: Calendar = .current) -> Int? {
calendar.dateComponents([component], from: self, to: date).value(for: component)
}
public func distance(from date: Date, only component: Calendar.Component, calendar: Calendar = .current) -> Int {
let days1 = calendar.component(component, from: self)
let days2 = calendar.component(component, from: date)
return days1 - days2
}
public func hasSame(_ component: Calendar.Component, as date: Date) -> Bool {
self.distance(from: date, only: component) == 0
}
}
example for use:
let dateOne = Date() // or any date
let dateTwo = getDateFromServer // your second date for
// option One
let distanceDay = dateOne.fullDistance(from: dateTwo, resultIn: .day)
var cal = Calendar.current // for your calendar
cal.locale = Locale.init(identifier: "EN")
// option Two
let distanceDay = dateOne.fullDistance(from: dateTwo, resultIn: .day, calendar: cal)
you can set hour, minute or any Component for find difference instead of day in result parameter

How to group array of dates by months in swift?

I am working on a scheduling app. I want all the dates of the given months I am not able to group dates by months that is what I tried but I want a different expected result
extension Date {
static func dates(from fromDate: Date, to toDate: Date) -> [Date] {
var dates: [Date] = []
var date = fromDate
while date <= toDate {
dates.append(date)
guard let newDate = Calendar.current.date(byAdding: .day, value: 1, to: date) else { break }
date = newDate
}
return dates
}
var month: Int {
return Calendar.current.component(.month, from: self)
}
}
let fromDate = Calendar.current.date(byAdding: .day, value: 30, to: Date())
let datesBetweenArray = Date.dates(from: Date(), to: fromDate!)
var sortedDatesByMonth: [[Date]] = []
let filterDatesByMonth = { month in datesBetweenArray.filter { $0.month == month } }
(1...12).forEach { sortedDatesByMonth.append(filterDatesByMonth($0)) }
The result is in this format [[], [], [], [], [], [], [2019-07-31
03:51:19 +0000],……., [], [], [], []]
This kinda result I want expecting
struct ScheduleDates {
var month: String
var dates: [Date]
init(month: String, dates: [Date]) {
self.month = month
self.dates = dates
}
}
var sections = [ScheduleDates]()
If you want to group your dates by month you can create a Dictionary like this:
Dictionary(grouping: datesBetweenArray, by: { $0.month })
This results in the following output of the format [Int: [Date]]
The key of the dictionary will be your month.
Now you can initialize your scheduleDates struct by looping through this dictionary in this way:
var sections = Dictionary(grouping: datesBetweenArray,
by: ({$0.month}))
.map { tuple in
ScheduleDates(month: String(tuple.0), dates: tuple.1)
}
Here's the code for a Playground
I think your structs should probably be Int values for the months, as when you go to populate something like a tableview, it'll be a PITA to re-order months if you've got them as Strings.
struct ScheduleDates {
var month: String
var dates: [Date]
}
Anyway, here's the extension I wrote based on what you provided. I frankly think you should return a dictionary with an Int as the key and an array of Dates as the value, but here's what you wanted...
I used Dictionary(grouping:by:) to construct a dictionary from an array of dates.
extension Date {
static func dateDictionary(from arrayOfDates: [Date]) -> [String: [Date]] {
// declare a dictionary
return Dictionary(grouping: arrayOfDates) { date -> String in
// get the month as an int
let monthAsInt = Calendar.current.dateComponents([.month], from: date).month
// convert the int to a string...i think you probably want to return an int value and do the month conversion in your tableview or collection view
let monthName = DateFormatter().monthSymbols[(monthAsInt ?? 0) - 1]
// return the month string
return monthName
}
}
}
Here's a utility method I wrote to generate data while I figured out how to do it. If you're going to be in production, don't force unwrap stuff as I did here.
// Utility method to generate dates
func createDate(month: Int, day: Int, year: Int) -> Date? {
var components = DateComponents()
components.month = month
components.day = day
components.year = year
return Calendar.current.date(from: components)!
}
Below is how I generated an array of sample dates to experiment.
// generate array of sample dates
let dateArray: [Date] = {
let months = Array(1...12)
let days = Array(1...31)
let years = [2019]
var dateArray: [Date] = []
while dateArray.count < 100 {
let randomMonth = months.randomElement()
let randomDay = days.randomElement()
let randomYear = years.randomElement()
if let month = randomMonth,
let day = randomDay,
let year = randomYear,
let date = createDate(month: month,
day: day,
year: year) {
dateArray.append(date)
}
}
return dateArray
}()
let monthDictionary = Date.dateDictionary(from: dateArray)
var arrayOfStructs: [ScheduleDates] = []
monthDictionary.keys.forEach { key in
let scheduleDate = ScheduleDates(month: key,
dates: monthDictionary[key] ?? [])
arrayOfStruct.append(scheduleDate)
}
print(arrayOfStructs)
You can use my code. Which i write to adapt your case.
let fromDate = Calendar.current.date(byAdding: .day, value: 30, to: Date())
let datesBetweenArray = Date.dates(from: Date(), to: fromDate!)
if datesBetweenArray.count <= 1 {
print(datesBetweenArray)
}
var sortedDatesByMonth: [[Date]] = []
var tempMonth = datesBetweenArray[0].month
var dates: [Date] = []
for i in 0..<datesBetweenArray.count {
if tempMonth == datesBetweenArray[i].month {
dates.append(datesBetweenArray[i])
if i == datesBetweenArray.count - 1 {
sortedDatesByMonth.append(dates)
}
} else {
sortedDatesByMonth.append(dates)
tempMonth = datesBetweenArray[i].month
dates.removeAll()
dates.append(datesBetweenArray[i])
}
}
print(sortedDatesByMonth.count)
print(sortedDatesByMonth)

How to get a half of every month from current year in Swift

In my application i have an option to enter the data for every 15days.I have to maintain this for an current year.Please help me to figure out this problem.
For ex: [
"1-1-2018 to 15-1-2018", "16-1-2018 to 31-1-2018",
"1-2-2018 to 15-2-2018", "16-2-2018 to 28-2-2018",
"1-3-2018 to 15-3-2018", "16-3-2018 to 31-3-2018",
"1-4-2018 to 15-4-2018", "16-4-2018 to 30-4-2018",
"1-5-2018 to 15-5-2018", "16-5-2018 to 31-5-2018",
"1-6-2018 to 15-6-2018", "16-6-2018 to 30-6-2018",
"1-7-2018 to 15-7-2018", "16-7-2018 to 31-7-2018",
"1-8-2018 to 15-8-2018", "16-8-2018 to 31-8-2018",
"1-9-2018 to 15-9-2018", "16-9-2018 to 30-9-2018",
"1-10-2018 to 15-10-2018", "16-10-2018 to 31-10-2018",
"1-11-2018 to 15-11-2018", "16-11-2018 to 30-11-2018",
"1-12-2018 to 15-12-2018", "16-12-2018 to 31-12-2018"
]
From Calendar API you can get total number of days for any month in the given date and also first day of the month like below,
extension Calendar {
public func firstDayOfMonth(date: Date) -> Date {
let components = self.dateComponents([.year, .month], from: date)
return self.date(from: components) ?? date
}
public func numberOfDaysInMonthFor(date: Date) -> Int {
let range = self.range(of: .day, in: .month, for: date)
return range?.count ?? 0
}
public func lowerHalfOfMonthFor(date: Date) -> (Date, Date) {
let startDate = self.firstDayOfMonth(date: date)
let endDate = startDate.dateByAppending(day: 14)
return (startDate, endDate)
}
public func upperHalfOfMonthFor(date: Date) -> (Date, Date) {
let firstDayOfMonthDate = self.firstDayOfMonth(date: date)
let totalNoOfDaysInMonth = self.numberOfDaysInMonthFor(date: firstDayOfMonthDate)
let startDate = firstDayOfMonthDate.dateByAppending(day: 15)
let endDate = firstDayOfMonthDate.dateByAppending(day: totalNoOfDaysInMonth - 1)
return (startDate, endDate)
}
}
you can also extend Date to get new date by appending any number of days,
extension Date {
public func dateByAppending(day: Int) -> Date {
let newDate = Calendar.current.date(byAdding: .day, value: day, to: self)
return newDate ?? self
}
public func daysDifference(_ date: Date?) -> Int? {
guard let date = date else { return nil }
return Calendar.current.dateComponents([.day], from: self, to: date).day
}
With the mix of above helper methods, you should be able to achieve the required result like below,
let date = Date()
let lowerHalf = Calendar.current.lowerHalfOfMonthFor(date: date)
let uppperHalf = Calendar.current.upperHalfOfMonthFor(date: date)
Below code will calculate the 15th day if you give the input,
static func getFortnightly(selectedDate : String) -> String?{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yy" //Your date format
if let dateSelected = dateFormatter.date(from: selectedDate) {//according to d
let newDate = Calendar.current.date(byAdding: .weekOfYear, value: 2, to: dateSelected)
let convertedDateToString = dateFormatter.string(from: newDate!)
return convertedDateToString
}
return nil
}
}

Swift Problems understanding the result of Calendar.dateComponents

in my app I have a list of contacts including the contacts birthday.
Now I want to see which of my contacts have birthday within in the next 7 days.
I am using a filter on my list to filter and return only thos contacts that match
let timeSpan = 7
let cal = Calendar.current
let now = Date()
var birthDays = contactList.filter { (contact) -> Bool in
if let birthDate = contact.birthDate {
let difference = cal.dateComponents([.day,.year], from: birthDate as Date, to: now! )
print("BD:\(birthDate) : DIFF \(difference.day!)")
return ((difference.day! <= timeSpan) && difference.day! >= 0)
}
return false
}
My hope was so. However the result was weired. So I added that ugly print into my closure in order to see the result of 'difference'
What is odd is that for instance the following:
Today is 2017-08-18 one of my contacts was born on 1987-08-19.
So instead of returning a difference.day of 1 I receive 364. If I swap from: and to: in the dateComponents I receive difference.day of -364.
My expectation was to have a difference.day = 1.
Again in Playground
import UIKit
var now = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let s = dateFormatter.date(from: "1987-08-19")
let cal = Calendar.current
let difference = cal.dateComponents([Calendar.Component.day,Calendar.Component.month,Calendar.Component.year], from: s!, to: now )
print("\(difference.day!)") // result is difference.day = 30
What am I doing wrong?
Thanks
You can create an extension to return the number of days from the next birthday as follow:
extension Date {
var year: Int { return Calendar.current.component(.year, from: self) }
var month: Int { return Calendar.current.component(.month, from: self) }
var day: Int { return Calendar.current.component(.day, from: self) }
var noon: Date { return Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: self)! }
var daysFromBirthday: Int {
let nextBirthDate = DateComponents(calendar: .current, year: Date().year + (month < Date().month ? 1 : 0), month: month, day: day, hour: 12).date ?? Date.distantFuture
return Calendar.current.dateComponents([.day], from: Date().noon, to: nextBirthDate).day ?? 0
}
}
And you can now filter your objects as follow:
let timeSpan = 0...7
let birthDays = contactList.filter {
timeSpan ~= $0.birthDate?.daysFromBirthday ?? -1
}
The problem was you were checking the difference of the days from the user's birthday but in the app we will need to get the difference between the current date and the birthday during this year. Say in the example that you have said, it's the difference between 2017-08-19(this year's birthday) and 2017-08-18(current date). You could try the code below.
var now = Date()
dateFormatter.dateFormat = "yyyy-MM-dd"
let s = dateFormatter.date(from: "1987-08-19")
let cal = Calendar.current
let currentDateComponentsYear = cal.dateComponents([.year,.month,.day], from: newDate!)
var dateComponents = cal.dateComponents([.year,.month,.day], from: s!)
dateComponents.year = currentDateComponentsYear.year
let currentYearBDay = cal.date(from: dateComponents)
currentDateComponentsYear.month
let difference = cal.dateComponents([Calendar.Component.year,Calendar.Component.month,Calendar.Component.day], from:newDate! , to: currentYearBDay! )
let daysDiffernce:Int?
if dateComponents.month == 1 && currentDateComponentsYear.month == 12 && difference.day! < 0 {
let range = cal.range(of: .day, in: .month, for: newDate!)!
let numDays = range.count
daysDiffernce = difference.day! + numDays
} else {
daysDiffernce = difference.day
}
print("\(daysDiffernce!)") //result 1

How to get week start date and end date by using any Month and week number swift 3?

I have to implement graph so that I need to get week start date and weekend date if I will pass the date object and week number.
How can I achieve that I tried it but didn't get exactly?
Here below is my code:-
Weekday:-
//Day of week
func getDayOfWeek(today:String)->Int? {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
if let todayDate = formatter.date(from: today) {
let myCalendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)!
let myComponents = myCalendar.components(.weekday, from: todayDate)
let weekDay = myComponents.weekday
return weekDay
} else {
return nil
}
}.
extension Date {
var millisecondsSince1970:Int {
return Int((self.timeIntervalSince1970 * 1000.0).rounded())
}
init(milliseconds:Int) {
self = Date(timeIntervalSince1970: TimeInterval(milliseconds / 1000))
}
func startOfWeek(weekday: Int?) -> Date {
var cal = Calendar.current
var component = cal.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)
component.to12am()
cal.firstWeekday = weekday ?? 1
return cal.date(from: component)!
}
func endOfWeek(weekday: Int) -> Date {
let cal = Calendar.current
var component = DateComponents()
component.weekOfYear = 1
component.day = -1
component.to12pm()
return cal.date(byAdding: component, to: startOfWeek(weekday: weekday))!
}
}
internal extension DateComponents {
mutating func to12am() {
self.hour = 0
self.minute = 0
self.second = 0
}
mutating func to12pm(){
self.hour = 23
self.minute = 59
self.second = 59
}
}
This returns start- and end date for a given week number and date
func dayRangeOf(weekOfYear: Int, for date: Date) -> Range<Date>
{
let calendar = Calendar.current
let year = calendar.component(.yearForWeekOfYear, from: date)
let startComponents = DateComponents(weekOfYear: weekOfYear, yearForWeekOfYear: year)
let startDate = calendar.date(from: startComponents)!
let endComponents = DateComponents(day:7, second: -1)
let endDate = calendar.date(byAdding: endComponents, to: startDate)!
return startDate..<endDate
}
print(dayRangeOf(weekOfYear: 12, for: Date()))
Consider that print displays the dates in UTC and the start date depends on the first weekday setting of the current locale.
Edit
A version to determine the range of a given week of month
func dayRangeOf(weekOfMonth: Int, year: Int, month: Int) -> Range<Date>? {
let calendar = Calendar.current
guard let startOfMonth = calendar.date(from: DateComponents(year:year, month:month)) else { return nil }
var startDate = Date()
if weekOfMonth == 1 {
var interval = TimeInterval()
guard calendar.dateInterval(of: .weekOfMonth, start: &startDate, interval: &interval, for: startOfMonth) else { return nil }
} else {
let nextComponents = DateComponents(year: year, month: month, weekOfMonth: weekOfMonth)
guard let weekStartDate = calendar.nextDate(after: startOfMonth, matching: nextComponents, matchingPolicy: .nextTime) else {
return nil
}
startDate = weekStartDate
}
let endComponents = DateComponents(day:7, second: -1)
let endDate = calendar.date(byAdding: endComponents, to: startDate)!
return startDate..<endDate
}
print(dayRangeOf(weekOfMonth: 5, year: 2017, month: 6))
The result type of the second version is an optional because there are a few calculations which could fail for example if the number of week in the particular month is out of range.
For anyone interested in this, it looks like OP confusing weekOfMonth and weekOfYear…
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
let cal = Calendar.current
let dateComponents = DateComponents(year: 2018, month: 3, day: 15)
let date = cal.date(from: dateComponents)!
func weekOfMonthStart(forDate date: Date) -> Date {
var compsToWeekOfMonth = cal.dateComponents([.year, .month, .weekOfYear], from: date)
compsToWeekOfMonth.day = cal.range(of: .day, in: .weekOfMonth, for: date)?.lowerBound
return cal.date(from: compsToWeekOfMonth)!
}
Somebody mention an answer that will fail, so a test was included ;)
for i in 0...5000 {
let newDate = cal.date(byAdding: DateComponents(day:i), to: date)!
weekOfMonthStart(forDate: newDate)
}

Resources