Unable To Set Date in DatePicker Swift - swift

I have a common datePicker,
where in Part 1 of the code (which is executed when datePicker is changed) I am setting datePicker's minimumDate and maximumDate and this works.
In Part 2 of the code (which is executed when cell is tapped) I am only setting the datePickers date.
When Part 1 is followed by Part 2 of the code I am unable to set datePickers date i.e the datePicker shows the maximumDate set (which is done in part 1 of the code)
When Part 2 of the code executed without part 1, I am able to set the datePickers date.
Can someone please advice where I could be going wrong ?
Part 1
let endTimeString = self.timesArray[row]["endTime"]!
let endTimeObject = self.convertToDateObject(timeString: endTimeString)
let maxStartTimeObject = endTimeObject.addingTimeInterval(-5 * 60.0) // removing 5mins
cell.datePicker.maximumDate = maxStartTimeObject
let startTimeString = self.timesArray[row]["startTime"]
let startTimeObject = self.convertToDateObject(timeString: startTimeString!)
let minEndTimeObject = startTimeObject.addingTimeInterval(5 * 60.0) // adding 5mins
cell.datePicker.minimumDate = minEndTimeObject
Part 2
cell.datePicker.date = dateObject

Is it possible that the date you are trying to set it outside your min/max bounds?

Related

Use 2017-02-12T13:30:00Z date in angular2-simple-countdown/countdown

I want to make a Count down timer using Angular 2 .
I am able to get the Timer using the ans provided to work but i am not able to override the Style of the Timer the Size of the timer i pretty big.
Using like below in template.
<count-down [text]="text" units="Days | Hours | Minutes | Seconds " end="{{fixture.date}}"></count-down>
howto binding data to your CountDownTimer component, your date from api could be using like this
let d = new Date('2017-02-12T13:30:00Z');
let year = d.getFullYear();
let month = d.getMonth();
let day = d.getDate();
from there you could binding any property you want.

Retrieve date from double value in NSUserDefaults (Swift)

Hi I'm creating an that has two controls: A NSSlider and a NSTextField that update continuously. I added a NSDateFormatter to the NSTextField so that it shows the current value of the slider formatted as a time. The slider has a range of 0 to 86,400 units (so that the user slides the seconds in a range of 24 hours).
I connected my app to the shared user defaults controller so that the value is stored for the user.
The app shows the correct time in the NSTextField, but it always stores the value in seconds. for example, if the user selected 06:32:14 when moving the slider, the system will actually store 23534.2261709793 in the key startTime
When the user opens the app again, they will see 06:32:14 as expected.
My problem is when I want to read the variable in my code:
I can read the value as Double:
let value = NSUserDefaults.standardUserDefaults().objectForKey("startTimeValue") as? Double
This works fine, if I try to cast it as NSDate it won't work (the value will be set to nil). So I thought that using a NSDateFormatter in code could work (as this is the way interface builder does the work for the user to see the date):
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.NoStyle
dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle
let date = dateFormatter.dateFromString((value?.description)!)
In the previous code I created the date formatter, then set the date style to nothing (I only want to do the time), and the time style to short. But giving the value.description ("23534.2261709793") still won't work. date will still be nil.
Is there something I'm doing wrong?
The double that you are storing / getting from NSUserDefaults is the time interval from Thursday 1st January 1970. So you need to feed that time interval into the NSDate constructor so that it can construct the date for you.
let value = NSUserDefaults.standardUserDefaults().objectForKey("startTimeValue") as? Double
let date = NSDate(timeIntervalSince1970: value)

Set maximum time with UIDatePicker - time mode - swift

Is it possible to customise the UIDatePicker in CountDownTimer mode to not show the picker values past a certain point? For example if I set it to 2 hours it would only show 2 hours downwards.
Currently I have tried a few techniques but all I have succeeded in is setting the countdown point to a certain value using maximumDate.
self.datePickerView.datePickerMode = UIDatePickerMode.CountDownTimer
//For calculating a date with +30 mins
let calendar = NSCalendar.currentCalendar()
let date = calendar.dateByAddingUnit(.CalendarUnitMinute, value: 30, toDate: NSDate(), options: nil)
println("30+Mins \(date)")
//Interval for countdownDuration
let myTimeInterval = NSTimeInterval(timeOffset * 60)
println("myTimeInterval \(myTimeInterval)")
//Tried Methods - countDownDuration sets it to a certain points(in seconds)
self.datePickerView.countDownDuration = myTimeInterval
self.datePickerView.maximumDate = date // This doesn't seem to do anything when in using UIDatePickerMode.CountDownTimer
I presume a possible way is to use just a UIPicker.
Exactly as you say - it's possible to do it yourself with UIPickerView. And that's actually exactly what I did: I built a custom UIPickerView subclass for that purpose. It replicates the functionality of UIDatePicker in countDownTimer mode, while adding support to set maxTimeInterval.
You use it like this:
GSTimeIntervalPicker *picker = [[GSTimeIntervalPicker alloc] init];
picker.maxTimeInterval = (3600 * 3); // set the limit
picker.minuteInterval = 5; // the step. Default is 1 min.
picker.timeInterval = (3600 * 1.5); // 1 h 30 minutes
picker.onTimeIntervalChanged = ^(NSTimeInterval newTimeInterval) {
// Use the value
};
Available on GitHub under MIT license. Blog post here. I'm sorry it's not in Swift, you will have to convert it yourself.
First Thing: you don't need a calendar to add 30 minutes to a given date.
There is a constructor that does what you want in one step:
NSDate(timeInterval: NSTimeInterval, sinceDate: NSDate)
NSDate(timeIntervalSinceNow: NSTimeInterval)
for me,
self.datePickerView.maximumDate = date
shows future dates / time greyed out, so the user has some visual hint.
This doesn't mean, a future date / time is not selectable without additional code.
The user can select a greyed out time. You can check in your handler and when some future date is selected, you just set
self.datePickerView.date = date
as long as you don't call resignFirstResponder(), your date picker scrolls back to now (or any other chosen time) nicely. So the user is not able to select a time in the future with this additional code.
I hope this is near enough to what you need. Future is greyed out instead of invisible and it is not selectable with a little additional code in your handler.

AppleScript (or swift) add hours to time

I'm trying to add 8Hours to a date (from the clipboard)
set date1 to the clipboard
set newdate to date1 + (8 * hours)
display dialog "Purchases were downloaded at " & newdate buttons {"OK"} default button 1
But this is not working as expected, I'm having the error
Can’t make "06/22/2015 08:15:27 " into type number.
You need to pick the hours as an individual variable, like shown below:
set currentDate to current date
set newHour to ((hours of currentDate) + 8)
You can also use this for days, minutes and seconds.
This will work. You can then use the variables to construct a new date to be used in the display dialog.
PS. Don't forget to change the day if the newHour variable is bigger than 24 hours.
EDIT
Setting a date to the clipboard can be done like this:
set currentDate to current date
set the clipboard to currentDate as Unicode text
Getting the current clipboard and adding it to a variable goes like this:
set currentDate to get the clipboard
display dialog currentDate
I hope this helps!
In Swift, you can call dateByAddingTimeInterval on an NSDate object.
The time interval is measured in seconds.
yourDate.dateByAddingTimeInterval(8 * 60 * 60)
If you wanted to add another method to add 8 hours directly, you could define an extension:
extension NSDate {
func addEightHours() -> NSDate {
return self.dateByAddingTimeInterval(8 * 60 * 60)
}
}

Parse.com — Query with a date object in Swift

A parse object in my database has a date field. I want to use a date that the user selects as the query.
query.whereKey("dateFieldParse", equalTo: janFirst)
I'm not sure how to set up this "janFirst" field. I know it has to be somehow formatted for parse retrieval but what is this format.
I tried setting the components in an NSDate object but it doesn't seem to work.
let userCalendar = NSCalendar.currentCalendar()
let janFirstComponent = NSDateComponents()
janFirstComponent.year = 2014
janFirstComponent.month = 1
janFirstComponent.day = 1
janFirstComponent.hour = 0
janFirstComponent.minute = 0
var janFirst: NSDate = userCalendar.dateFromComponents(janFirstComponent)!
....
query.whereKey("dateFieldParse", equalTo: janFirst)
One more small thing: is the hours and seconds, and even the year, necessary for retrieval? I'm only really using the month and date.
The code for Parse and Swift still seems to be very limited and I can't seem to find anything at all for retrieving dates (+ I'm still very much a iOS/swift noob). Any help, even some general steps as to what to do would be greatly appreciated.
Thanks in advance. :)
I actually had it right, I just didn't realize timezones were an issue with Parse. It seems the timezone had to be set to "GMT" (Parse's timezone). For anyone that might face a similar issue, the wee bit of code is below.
let userCalendar = NSCalendar.currentCalendar()
let timeZone = NSTimeZone(abbreviation: "GMT")
userCalendar.timeZone = timeZone!
In your Parse data browser, make sure your parse object is set to Date and you variable is set to NSDate