UIDatePicker doesn't print out a date - ios

I implemented a UIDatePicker into my app. If I change the default date after the date picker was popping up, the date picker gives a valid date back. But if I'd like to choose the current date (I don't have to change the date picker) the date picker doesn't give a date back. Does someone knows a solution for this in Swift?
//Date Picker config.
func datePickerChanged(datePicker:UIDatePicker) {
var dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
var strDate = dateFormatter.stringFromDate(datePicker.date)
dateLabel.text = strDate
}

You have a callback when you change the date (valueChanged). So it is normal you don't get it when you just dismiss the picker without changing the date.
You have to know the date before you present your date picker, in case you don't set it, or if you want to cancel your selection if you handle it.
Create a var (I generally call it now ) where you store the currentDate (NSDate()) and set it to the date property of your UIDatePicker, so you know the date when the picker was presented since the default datePicker's date displayed is now.

If you set UIDatePicker's date in code, it doesn't run valueChanged events. You have to extract the date string update into a separate method and update it directly.
Also, don't treat the UIDatePicker as the sole date value source. Keep the model separate.
i.e.
var date = NSDate()
func datePickerChanged(datePicker:UIDatePicker) {
self.date = datePicker.date
self.updateDateStringWithDate(self.date)
}
func resetDate() {
self.date = NSDate()
self.datePicker.date = self.date
self.updateDateStringWithDate(self.date)
}
func updateDateStringWithDate(date: NSDate) {
var dateFormatter = UIDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
var strDate = dateFormatter.stringFromDate(date)
dateLabel.text = strDate
}
Also as a side note, don't create a new UIDateFormatter every time it's needed, because its initialisation is relatively expensive.

Related

Disable future dates selection in FScalendar swift

I am using https://github.com/WenchaoD/FSCalendar in my project . MaximumSelectedDate is a read-only property .Then how can disable future dates ?
You should be using the delegate method to address this
func maximumDate(for calendar: FSCalendar) -> Date {
return Date()
}
A workaround could be to edit FSCalendar method file. First make a bool variable, say isAllowedToLimitFutureDates and a string variable maxValidFutureDateAsString then change line 172 of this link to:
if(!isAllowedToLimitFutureDates)
{
_maximumDate = [self.formatter dateFromString:#"2099-12-31"];
}
else
{
_maximumDate = maxValidFutureDateAsString; // say "2017-03-13"
}
So when you want to limit the dates set isAllowedToLimitFutureDates = true.
Similar approach to line 1707.
In case you cannot edit file and used PODs, then you can customize this control and override them.
Hope that helps!
#Devraj answer is correct, there are delegates for both minimum and maximum dates, all you need to do is implementing the proper one (the later one in your case) in the controller that's conforming to FSCalendarDelegate and that'll do the trick.
func maximumDateForCalendar(calendar: FSCalendar) -> NSDate {
return NSDate() // NSDate of your choosing here
}
for Swift 3
fileprivate lazy var dateFormatter2: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy"
return formatter }()
let today = dateFormatter2.string(from: calendar.selectedDate!)
let dateObj = dateFormatter2.date(from: today)
if dateObj! > calendar.today! {
// Your logic here
}

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.

UIDatePicker min and max date not changing

I'm using Xcode 8.0 and building on iOS 10 with Swift 3.
I have UIDatePicker in my view controller, connected as an outlet :
#IBOutlet weak var datePicker: UIDatePicker!
I'm trying to set maximum and minimum dates for it. The code I'm using :
self.datePicker.minimumDate = Date()
self.datePicker.maximumDate = Date(timeIntervalSinceNow: 60*60*24*365)
self.datePicker.datePickerMode = UIDatePickerMode.date
is not working - date picker can go infinitely back and forward in time.
Question is - what am I missing here?
I unfortunately suffered the same issue literally yesterday and after searching SO couldn't find a solution. I came up with my own implementation of max/min date to use in place of the built in one, it's not ideal, but it works.
func datePickerDidChangeDate(sender: UIDatePicker) {
let maxDate = Date(timeIntervalSinceNow: 60*60*24*365)
let minDate = Date()
if sender.date.compare(minDate) == .orderedAscending || sender.date.compare(maxDate) == .orderedDescending {
// Date is invalid
sender.setDate(Date()) // Set selected date to now.
return
}
}
Also note doing the whole 60*60*24*365 is a bad idea and you should instead use NSCalendar.dateByAddingComponents but that is an entirely different question altogether.

How to set a UITextField.text to a NSDate?

I'm connecting to an API and when I get data back for dates it looks like this:
2016-07-05T21:39:17.696Z
How can I set a uitextfield.text to that date so it looks like a normal date (ie. 07/05/2016 or something similar)?
The tricky part is the NSDate is returned as a string from the API so it's not in the NSDate format to begin with.
A text field is only for editing freeform text, not dates. You can go one of two routes. If you intend for the user to be able to edit a date, then use a UIDatePicker to provide an interface specifically for editing dates. If you just want freeform text back, then you can convert a date to a string with NSDateFormatter.
try:
let formatter = NSDateFormatter()
let yourDate = NSDate()
formatter.dateStyle = NSDateFormatterShortStyle
formatter.timeStyle = NSDateFormatterNoStyle
self.textfield.text = formatter.stringFromDate(yourDate)

Writing time to subtitle text

I'm writing an app with swift like list app. When i create a new task everything is working fine but a want to write a creation time on Subtitle text of table view's cell. How can i do that?
You can use NSDate to get the current date and time.
So in your creation method, (or object init):
let date = NSDate()
let dateFormatter = NSDateFormatter().Shortstyle
let dateString = dateFormatter.stringFromDate(date)

Resources