NSDate from week number - ios

I am trying to create a weekly recurring local notification using Swift. Notification is going to be set to first day of every week, and in 10:00. Ofcourse I don't want to set a notification for a past date, so I need to see if now it has passed first day of week 10:00. If not, I will create notification for today 10:00, else next week monday 10:00.
I created an extension to calculate week number for current date.
extension NSDate {
func weekOfYear() -> Int {
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(NSCalendarUnit.WeekOfYear, fromDate: self)
let weekOfYear = components.weekOfYear
return weekOfYear
}
}
I couldn't go any further than this. Any help is appreciated.
Regards.

There is an easier solution to your problem. You can use the nextDateAfterDate() method of NSCalendar:
let cal = NSCalendar.currentCalendar()
let comps = NSDateComponents()
comps.hour = 10
comps.weekday = cal.firstWeekday
let now = NSDate()
let fireDate = cal.nextDateAfterDate(now, matchingComponents: comps, options: [.MatchNextTimePreservingSmallerUnits])!
This gives the first date in the future which is at 10:00 on the first day of the week.

Related

How to disable specific days in UIDatePicker

I have a UIDatePicker that should show only specific days, like all Sundays and Mondays, and disable all other dates, The user can select only Sunday and Monday from this week, next week, the week after, to, let's say, 2020.
Is that even possible with UIDatePicker, or even any custom control?
I would suggest you to use like this:
From below code you will get dates of all the Sundays from 2016-2018,
let cal = Calendar.current
// Get the date of 2 years ago today
let stopDate = cal.date(byAdding: .year, value: -2, to: Date())!
// We want to find dates that match on Sundays at midnight local time
var comps = DateComponents()
comps.weekday = 1 // Sunday
// Enumerate all of the dates
cal.enumerateDates(startingAfter: Date(), matching: comps, matchingPolicy: .previousTimePreservingSmallerComponents, repeatedTimePolicy: .first, direction: .backward) { (date, match, stop) in
if let date = date {
if date < stopDate {
stop = true // We've reached the end, exit the loop
} else {
print("\(date)") // do what you need with the date
}
}
}
The logic is simple. Make an array of this is in your required dateFormat and show that in pickerView which will eventually work as datePicker for you.
Hope this helps.
This is impossible. You have to create custom date picker based on UIPickerView.
There is a way to do it with just the UIDatePicker!
Lets say you have a minTime that is January 16 10:00AM
You would do something like this:
let picker = UIDatePicker()
picker.minimumDate = minTime
picker.maximumDate = minTime.changing(.hour, value: 23).changing(.minute, value: 59)
Then the picker will be stuck to that date.
Hope this helps someone!

How to get the specific date in iOS?

I'm working on a project where I have labels and images that change on a daily basis. My idea on how to go about this is I add assets for the images and have multiple files where I take the text (that contains the quote).
Two questions:
Is there a better way of approaching this? (I'm fairly new to iOS, so I'm wondering if this is sound).
How can I take the current day as in: May 22? (I just want to know how to get "22").
you need to use NSDate
// Option 1
let date = NSDate() // today
let dateStringFormatter = NSDateFormatter()
dateStringFormatter.dateFormat = "dd"
let d = dateStringFormatter.stringFromDate(date)
// d = "22"
// Option 2
let d = NSCalendar.currentCalendar().component(.Day, fromDate: NSDate()) // thanks Leo-Dabus
Reference: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/
Try taking a look at this stackoverflow link on getting the current date.
How to get the current time as datetime
According to noliv in the article you should be able do :
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute, fromDate: date)
let hour = components.hour
let minutes = components.minute
Hope this helps

List of Days of this week

I need a list of days of the week (with the week starting on Monday) for a timecard list. Im not sure how to achieve this. I have tried some dateByAddingUnit and subtracting from there but it hasn't produced the results I want.
Any idea how to get a list of the dates? (i.e. Monday would be 3/28/16, Tuesday 3/29/16 and so on)
Edit: missed that you want a week to always start on a Monday
let gregorian = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let today = gregorian.startOfDayForDate(NSDate())
let startOfWeek: NSDate
if gregorian.component(.Weekday, fromDate: today) == 2 {
// Today is Monday
startOfWeek = today
} else {
// Find the last Monday prior to today
startOfWeek = gregorian.nextDateAfterDate(today, matchingUnit: .Weekday, value: 2, options: [.SearchBackwards, .MatchPreviousTimePreservingSmallerUnits])!
}

Swift 2.1 DatePicker setDate does not set date to current day and specified time

I am trying to set the minimum date to the current day on the date picker and then for the sake of user experience, set the time to 7pm or 19:00. For some reason it doesn't seem to be working. The minimum date setting works but the time doesn't get set. If I remove the minimum date line, the time gets set to 7pm, but strangely, the date gets set to Monday, Jan 1. Which isn't even this year.
Here's my code below:
datePicker.minimumDate = NSDate()
let calendar:NSCalendar = NSCalendar.currentCalendar()
let components = calendar.components([NSCalendarUnit.Hour, NSCalendarUnit.Minute], fromDate: NSDate())
components.hour = 19
components.minute = 00
datePicker.setDate(calendar.dateFromComponents(components)!, animated: true)
What am I doing wrong here?
Split that last line into:
let date = calendar.dateFromComponents(components)!
datePicker.setDate(date, animated: true)
Then look at the value of date. You'll find it is probably January 1, 2000 at the desired time.
Fix this by adding the year, month, and day components to:
let components = calendar.components([NSCalendarUnit.Hour, NSCalendarUnit.Minute], fromDate: NSDate())
In other words, you need:
let components = calendar.components([NSCalendarUnit.Year, NSCalendarUnit.Month, NSCalendarUnit.Day, NSCalendarUnit.Hour, NSCalendarUnit.Minute], fromDate: NSDate())

Output for components.minutes unexpected. iOS Swift

Hi I am trying to get the current hour and minute of the day using the following code:
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour, fromDate: NSDate())
// Concatenate time and minutes together
let hour = components.hour
let minute = components.minute
println(components.hour)
println(components.minute)
The output for both:
components.hour output: 0
components.minute output: 9223372036854775807
It is currently 00.30 where I am. But why does components.minute print such a long number that doesn't to me seem to be the minute 30.
Also if anyone can suggest another way of getting the current time as e.g. 13:00?
Any help is much appreciated thanks!
9223372036854775807 is NSUndefinedDateComponent value. This means that NSDateComponents object doesn't have the value.
You have to join desired NSCalendarUnits with |:
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute, fromDate: NSDate())
OR, from iOS 8, we have handy component(_:fromDate:) method to get just one component's value.
let now = NSDate()
let hour = calendar.component(.CalendarUnitHour, fromDate: now)
let minute = calendar.component(.CalendarUnitMinute, fromDate: now)

Resources