Swift: Calendar app - coloring more fields - ios

I am doing an app with calendar with swift. I want to costumize to look like this:
The problem is here :
func calendarView(_ view: YMCalendarView, dateRangeForEventAtIndex index: Int, date: Date) -> DateRange? {
if calendar.isDateInToday(date)
|| calendar.isDate(date, inSameDayAs: calendar.endOfMonthForDate(date))
|| calendar.isDate(date, inSameDayAs: calendar.startOfMonthForDate(date))
{
return DateRange(start: date, end: calendar.endOfDayForDate(date))
}
return nil
}
The code above is ok to color the first day of month, last, and the current day but i want to color ex: from 12.09.2018 to 15.09.2018 how to do? Help me please if you can.

How does your event model look like? I guess it has a start and an end date, right? Fetch the event object by index in dateRangeForEventAtIndex, then return DateRange(start: event.startDate, end: event.endDate).

you can use UserDefault to save that date where you want to start and then compare to your current date or etc if match then set color green else nil

Related

Angular material dateAdapter does not consider different hours/minutes/seconds as a different date

I'm building a time extension (hours/minutes/seconds) for angular material DatePicker (beause there aren't any good ones out there yet). The problem is when I update the time in the time component I use the DatePickers select method which is the only method to set dates programatically using a Date object.
The problem is Datepickers inner select function does not consider different Hours/Minutes/seconds as a different date, and when supplied with such scenarion it will not update its inner workings.
The following is the select function of Angular Material Datepicker:
MatDatepicker.prototype.select = function (date) {
var oldValue = this._selected;
this._selected = date;
// this will eveluate as false since only hours/minuts/seconds are different and not day/month/year
if (!this._dateAdapter.sameDate(oldValue, this._selected)) {
this._selectedChanged.next(date);
}
};
Inside the sameDate method they use a method called compareDate which check the two dates only by day and month & year:
DateAdapter.prototype.compareDate = function (first, second) {
return this.getYear(first) - this.getYear(second) ||
this.getMonth(first) - this.getMonth(second) ||
this.getDate(first) - this.getDate(second);
};
This means select method will not emit the new date to DatePicker's inner components & parts.
I am using a custom NativeDateAdapter and a custom MatDateFormats but since the above check want emit the new date these mechanism want be reached.
P.S
Everything works ok when the updated date has a different day/year/month including the custom time formating to include the time parameters.
I resolved this by simply re-implementing the base classes compareDate method using the custom NativeDateAdapter.
export class AppDateAdapter extends NativeDateAdapter {
format(date: Date, displayFormat: Object): string {
// format your dates
}
compareDate(first: Date, second: Date) {
// compare first/current date with second/previous date
// you can implement your logic here.
return 1;
}
}

Add custom holidays in FSCalendar

I am using FSCalendar and its displaying only default holidays like on Sunday (The weekend) and so I want to add my own holidays in it and change color.
So please anyone knows how to add custom holidays????
I think you have to maintain holidays list on code level like model class(Array) or you can use the database for storing holidays list. At the render time only adopt the FSCalendarDelegateAppearance delegate. This call a delegate function for every date.
See example i think it can help you,
var datesWithHolidays = ["2018/09/03", "2018/10/06", "2018/09/12", "2018/10/25"]
//This is the delegate method
func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, fillDefaultColorFor date: Date) -> UIColor? {
let dateString = self.dateFormatter1.string(from: date)
if self.datesWithHolidays.contains(dateString) {
return UIColor.green
}
return nil
}

How can i select dates programmatically and show on FSCalendar

I used FSCalendar(https://github.com/WenchaoD/FSCalendar) in my project. If user click repeat button, the events repeats everyday. I want to show it on calendar which is in my application. How can I do it?
If you want to select date then below method will be helpful,
where calendar is outlet of FSCalendar.
calendar.select(calendar.today)
And if you want to select multiple dates then the best way would be to use event dots, and that can be possible through below delegate method of FSCalendar Data source,
func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int
{
//number of dots you want to show
}
or if you really want to show dates selected, you can use below method smartly and returning different colours for dates that you want to select,
public func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, fillDefaultColorFor date: Date) -> UIColor?
{
if your date
{
//return the colour for your dates
}
else
{
//return default colour
}
}
Hope, this answer helps you.
For Remainder app you need to do or use following thing.
1.Realm or Core Data - for Data persistency.
2.local notification - to notify user that some event is occured.
you need to use UILocalNotification that have one property fireDate property.
I don't know how you're persisting that information (the scheduled date), what i can tell you is that you can easily add a dot (or more if you'd like, just change the number) with the following delegate:
`func calendar(calendar: FSCalendar, numberOfEventsForDate date: NSDate) -> Int {
let formatter = NSDateFormatter()
formatter.dateFormat = "MM-DD"
let date1 = formatter.stringFromDate(yourDate)
let date2 = formatter.stringFromDate(date)
return date1 == date2 ? 1 : 0
}`
This method is one of FSCalendar delegates and it iterates all days in the current month (each one of them represented in date variable). In the example above i'm comparing if the any given date is equal to yourDate variable which has to be a NSDate as well by transforming both of them into Strings via NSDateFormatter in Month-Day format so the comparison is equal for all months. If true, return 1 (this indicates 1 dot, you may change it for more if you want to), otherwise return 0 dots below the specific date.
This code is Swift 2.3 compliance.

How to find dates of specific day of current month in swift

I am using FSCalendar, and I want to highlight dates day wise as am getting Wednesday from server and i want to highlight all Wednesday to be selected on FSCalendar of current month or year. Please solve this. Thank You.
FSCalendar has a delegate for cases like this, it's called titleDefaultColorForDate and you can use it this way:
func calendar(calendar: FSCalendar, appearance: FSCalendarAppearance, titleDefaultColorForDate date: NSDate) -> UIColor? {
return calendar.gregorian.component(.Weekday, fromDate: date) == 4 ? UIColor.blueColor() : UIColor.blackColor()
}
Let me elaborate: What this delegate method does is return a UIColor based on the date that's passed by reference when the view loads, in other words, every time a month loads this method is called 30+ times with every single date in it. Here i made the condition to return true if the given date was equal to 4 since that's how Wednesday is represented by this helper
calendar.gregorian.component(.Weekday, fromDate: date) // calendar is my FSCalendar outlet.
To summarize, Wednesday are blue and every other week day is black.
PS: Notice Wednesday = 4 in my case since my week begins on Sunday, otherwise you must do this calculation based on your starting day.

Get elements from array where date is today

I have an array of objects where a property is date of type NSDate. The array includes future days and past days.
I want to get an array of elements from that array that only includes dates that are in today, not the last 24 hours.
I ordered the elements by descending (new to old):
allDashboards.sortInPlace({ $0.date!.compare($1.date!) == NSComparisonResult.OrderedDescending
I tried to do a for loop, check if date is yesterday and cut the array there, but it doesn't work as it may be no element with a yesterdays date:
for dash in allDashboards {
if (NSCalendar.currentCalendar().isDateInYesterday(dash.date!)) {
print(i)
allDashboards.removeRange(Range.init(start: 0, end: i))
break
}
i += 1
}
Is there a method to see if date is past a day instead of if the date is part of that day?
One-liner:
let todayDashboards = allDashboards.filter { NSCalendar.currentCalendar().isDateInToday($0.date!) }
You can use NSDateComponents to create the dates which represent the start and end of your acceptable range. These can be anything you want.
Once you have those dates, you can create an NSPredicate which matches dates > the start and < the end dates. This will generate a filtered array for you.
You could use isDateInToday to filter the content too.
What you must not do is to iterate the array and mutate it at the same time, which is what your current code does with allDashboards.removeRange inside the for loop.
You can filter all dates which are today with the filter function and isDateInToday of NSCalendar
let filteredDashboards = allDashboards.filter{ NSCalendar.currentCalendar().isDateInToday($0.date!) }

Resources