NSDate without timezone for a UIDatePicker - ios

I have a UIDatePicker which I'm using to choose the opening time of a shop.
The problem is that the date picker is changing the displayed time depending on the timezone, but I just want to show the time exactly as it appears in the NSDate.
For example, my NSDate is set as 2001-01-01T09:15:00Z. i.e. 9:15am on 1st Jan 2001.
I set my UIDatePicker date to this date, but when I open the app in Spain (which is GMT-1) I see 10:15am.
I'm not using a dateFormatter here - just regular NSDate. Is there a way of telling the UIDatePicker to ignore the timezone?
Or perhaps I shouldn't be storing the time as an NSDate because of the inherent timezone issues?

NSDatePicker has timeZone property - The time zone reflected in the date displayed by the date picker.
See if that will help you...

Related

Need a time object in one of my iOS app (Swift)

I am currently creating an app where I ask the user to input a time (through datePicker) and send the user a notification every day on that time.However, I noticed that datePicker only has an NSDate object, and was wondering if there was a Time object counterpart.Also, would this Time object be a good way of storing the time, or should I convert the hour and minutes to Integers for storage?
Thanks in advance!
NSDate also contains Time information within it. You can store the exact date and time using only NSDate.
NSDate is a generic representation independent of any time zone. According to the apple docs:
NSDate objects encapsulate a single point in time, independent of any particular calendrical system or time zone. Date objects are immutable, representing an invariant time interval relative to an absolute reference date (00:00:00 UTC on 1 January 2001)
NSDate is basically the number of seconds from the reference date mentioned above.
And yes NSDate is the best way for storing time and date information in your app.
Whenever you want to display the time, use NSDateFormatter to format the date and time into any format you desire and for any Time Zone you require.
Swift 3
The NSDate class has been renamed to Date in Swift 3

How to get current NSDate in current timezone? [duplicate]

This question already has answers here:
Get NSDate from NSDate adjusted with timezone
(2 answers)
Closed 6 years ago.
I need to get current time in NSDate, not swift. I tried to do this like on screenshot, but the final nsdate is in wrong timezone.
An NSDate does not have a time zone. It records an instant in time on planet Earth. It is DISPLAYED in a particular time zone.
An NSDateFormatter will convert between NSDate objects and date strings (in either direction).
If you install a time zone into your date formatter then it will convert dates to/from strings using that time zone. If you don't specify a time zone it will use the user's current time zone.
If you try to display an NSDate using the Swift print statement or NSLog then it will be displayed in UTC. Always.
The code you've posted that prints strDate, your date string you created using a date formatter, is correct. The line `print(localDate!) is meaningless.
To repeat: NSDate objects do not have a time zone. When you log them to the console, they will always be displayed in UTC. If you want to see them in your local time zone then you need to use date formatter like you are doing.

iOS - Does setDefaultTimeZone handle daylight saving and timezone offset automatically?

Server currently store date in UTC and in the app user has option to select any timezone.
[NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneWithName:selectedTimeZone]]
This will override the local timezone. Do I still need to handle anything in NSDateFormatter ?
setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"
I think I don't need to call setTimeZone in NSDateFormatter if I override defaulttimezone.
And [NSDate date] will then return proper current date or it will be absolute time ?
There are a few related questions here.
This will override the local timezone. Do I still need to handle anything in NSDateFormatter?
You don't need to set the time zone, because if you don't, it uses the default value when the date formatter was created.
I think I don't need to call setTimeZone in NSDateFormatter if I override defaulttimezone.
True, but if you ever change the default time zone, you need to create a new NSDateFormatter, because it doesn't notice that the default has changed. Or you could just change the date formatter's time zone any time you would change the default.
And [NSDate date] will then return proper current date or it will be absolute time ?
The results of [NSDate date] are completely unaffected by the default time zone. NSDate has no time zone, so you get the same results even if you change the default.
Does setDefaultTimeZone handle daylight saving and timezone offset automatically?
If the time zone uses daylight saving time, then yes. Otherwise no. That seems obvious but keep in mind that zones like GMT-0500 don't have daylight saving time even though zones like America/New_York do.

change date of NSDate while keeping the time same

I get current time or time stamp of some image. I have to change only date while the time should not be changed. For example I use [NSdate date] to get current date and time and store in an NSdate object that is "2014-01-10 09:58:47 +0000". Now change only the date part, keeping the time same as it is "2013-11-09 09:58:47 +0000"
How can I achieve that?
Convert the date into it's date components (which includes the time part), change the date part of the components to be for the new day, and create a new date based on these components.
dateByAddingDateComponents is also another way to do it.
It's all described in the Calendrical Calculations documentation.

Xcode - Connect a Date Picker and a Time Picker to the same NSDate

I have a Time Picker and a Date Picker.
How to get the time included with the date in same NSDate?
I need the time between 2 dates (including time) How?
I have the code for getting time between dates, i just need help to include the time.
By default, a UIDatePicker is set to allow the user to choose a Date and a Time. The mode can be set to either Date, Time or Date and Time. As long as the mode of your UIDatePicker is set to Date and Time it will set both the date and time in the linked NSDate object.
If you really want to have two separate UIDatePickers, one set to Date and the other to Time then you will need to link them to two different NSDate objects and then have code behind to combine the two NSDates into a single NSDate using something like the code here Problem combining a date and a time into a single NSDate

Resources