NSDate as function parameter in Swift - ios

I'm facing an issue if I pass NSDate as a function parameter.
My code is:
self.PassDate(responseDate) ; // Response date value = 2015-05-15T00:00:00
func PassDate(date:NSDate) {
// Here ideally date value should be same as responseDate value.. but date is coming as 2015-05-14T07:00:00 PDT
}
Why passing NSDate is changing the timezone/values?

Nothing is changing. Different ways of viewing dates present them using different time zones. The underlying date is not changed. If you view the date in the debugger it (usually) shows it in UTC.
Try logging the date outside your function and inside using println() calls. You should see the same value in both places if you view it the same way each time.

Related

XCUITest, UIDatePicker, adjustToPickerWheelValue

I'm writing an XCUITest. I have a view with a UIDatePicker on it (NOT a regular picker).
I'm trying to set the date using adjustToPickerWheelValue.
This crashes with the following error:
caught "NSInvalidArgumentException", "-[XCUIElement(XCUIElementTypePickerWheel) adjustToPickerWheelValue:]_block_invoke can only be called with elements of type XCUIElementTypePickerWheel, not valid for DatePicker."
This implies of course that this method is not available for the UIDatePicker.
I've gone through the apple site and it states pretty clearly that it IS supported.
I can't find any examples of this used for a date picker.
It's possible of course that I'm just not setting it right (I'm not sure how one would pass a string date, for example).
This is the code I'm trying:
XCUIElement *DOB;
XCUIElementQuery *DOBList = app.datePickers;
DOB = [DOBList elementBoundByIndex:0];
NSLog(#"DOB = %#\n", [DOB description]);
[DOB adjustToPickerWheelValue:#"SOME DATE GOES HERE?"];
BTW - description is of a DatePicker.
Printing [DOB value] just returns an empty string.
Any ideas?
For using UIDatePickers, I do like that:
XCUIApplication().datePickers.firstMatch.pickerWheels["CurrentValue"].adjust(toPickerWheelValue: "NewValue")
You have to do that for each picker of the UIDatePicker.

How to get currentUTC time in swift

I want to set count down timer in swift. I have an option is to get current time is Date() but this method is giving wrong date time when my device time set wrong.
Is it possible to get exact current UTC time in swift, so I will set count down timer with exact time remaining.
Thanks.
The Date class doesn't have a timezone, it's just a "point int the time line" that's the same for all the timezones. When you use a DateFormatter to convert a date to a string (or a string to a date) you can specify the timezone like this:
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
If you cannot trust the device date you will have to use a NTP service like this:
https://github.com/instacart/TrueTime.swift
https://github.com/jbenet/ios-ntp
Get Date and Time from Apple Server
Many times, I have faced the same issue if the user changed his current time then lot's of logic will disturb, Unfortunately, no luck because of Date() always returns your device date.
In well-known game Candy crush, We can not play it for a specific time if my points got over, But if I change device time to feature time then everything will be unlocked. This is the best example of your problem.
You can use below-given web service or your web service to achieve your requirements. Below are some
free API's which provides date and time.
Geonames
Timezonedb
TrueTime
In addition to other answers, you can write an extension for Date class to get formatted Data in specific TimeZone to make it as utility function for future use. Like
extension Date {
func dateInTimeZone(timeZoneIdentifier: String, dateFormat: String) -> String {
let dtf = DateFormatter()
dtf.timeZone = TimeZone(identifier: timeZoneIdentifier)
dtf.dateFormat = dateFormat
return dtf.string(from: self)
}
}
Now you can call it like
Date().dateInTimeZone(timeZoneIdentifier: "UTC", dateFormat: "yyyy-MM-dd HH:mm:ss");

SBDatePicker Its posssible to set minimum date and maximum date in swift?

I am new for developing the ios application, I have used SBPickerSelector in Cocapods framework for Selecting datePicker.
whether If possible to set the MinumDate and MaximumDate???
Try;
let picker: SBPickerSelector = SBPickerSelector.picker()
//Minimum Date
picker.datePickerView.minimumDate = NSDate(); // A date
//Maximum Date
picker.datePickerView.maximumDate = NSDate(); // An other date ;
//or picker.setMaximumDateAllowed(NSDate())
I do not understand what do you get from using SBDatePicker, it look like they simply wrap the original class UIDatePicker.
The problem of using this kind of wrappers is that they don't always have the full feature set of the original class and you count on their developers to keep up with Apple changes (which they don't).
If you simply use UIDatePicker you can set maximumDate and minimumDate, just like you asked.
Check out the UIDatePicker Class Reference
And here is an example of setting the minimum and maximum dates

How to fetch date in iOS from string object

I have stored NSDate in the DB using http://www.appcoda.com/sqlite-database-ios-app-tutorial/.
When i fetch the date back to NSString i get following during debugging. (As mentioned in tutorial even though i have a date picker and store value in NSDate, its actually stored as string in DB finally.
(lldb) po _itemPurchaseDate
(
)
Attached screenshot. So how do i fetch date and present it in a ui label in iOS?

Time picker shows wrong time iphone sdk

I have time picker in my app. I'm showing timePicker.date and the time is wrong.
NSLog is
NSLog (#"date : %#",[timePicker.date description]);
log is like date: 2012-02-07 17:00:01 +0000
i think that problem is in timezone
in ViewDidLoad i have this code
timePicker.locale = [NSLocale currentLocale];
timePicker.timeZone = [NSTimeZone localTimeZone];
but it isn't working...
Can somebody help me to solve my problem. Thanks
It is likely that the time picker is correctly applying time zone, but you're logging it using GMT rather than local time so that it just looks wrong. If you want to display the time returned from your time picker using local time, use NSDate's descriptionWithCalendarFormat:timeZone:locale: method, or use the NSDateFormatter class to get complete control over how your date is displayed.

Resources