last day (14 days later) » 

10:22 AM
0
Q: Inconsistent Dates when saved to UserDefaults.standard

SylarCould someone explain why I get inconsistent days; one always less what what was saved? // Global let SETTINGS = UserDefaults.standard ... ViewController: { // Connected IBOutlets: [...] let dateFormatter = DateFormatter() let datePicker = UIDatePicker() // Shortened ViewDidLoad: ...

 
2015-04-05 23:00:00 +0000 is UTC time and is April 06, 2015 (midnight) in your time zone. There is nothing wrong.
 
@MartinR Ohhhhh! I was about to say something like that. How to get UK time format and stick to it?
 
(NS)Date is an absolute point in time and has no time zone or format. To print the time in your local timezone, use a date formatter and convert it back to a string. – If you search for "NSDate wrong by some hours" then you'll find many similar questions with answers.
 
@MartinR I need to set the datePicker an initial date based on what was saved. That's where is the issue is.
 
Where is the problem? The first date is April 06, 2015 (midnight) in your time zone and setting the initial date to tx[0] should just work.
 
10:22 AM
@MartinR Need to set an initial date to datePicker: datePicker.date = tx[0] as! Date But the picker sees it as April 5, 2016 and not 2015 *I'll clear up the force unwrapping later
 
Well, that is strange. So datePicker.date is 2015-04-05 23:00:00 +0000 but the picker displays it as April 5, 2016 ? Are your sure that you did not mix up tx[0] and tx[1]?
 
@MartinR No mix up. See edit picture.
 
There are two dates: 2015-04-05 23:00:00 +0000 which is April 06, 2015, and 2016-04-04 23:00:00 +0000 which is April 05, 2016. Your date picker displays the second one.
 
If I understand you correctly, the datePicker will use the second date as the initial and not individually?
 
Sorry, I do not understand what you mean. If you assign datePicker.date = tx[0] as! Date then the date picker should display that date (which is in 2015). Are you assigning datePicker.date = tx[1] as! Date somewhere else?
 
10:23 AM
What I wanted, when the first text field is selected, the date picker show the year 2015, when second field is selected, it show the year 2016. Possible?
 
Sure, but that is a completely different problem now and has nothing to do with user defaults or a date being off by one hour.
 
I couldn't find the correct title for it. I thought the dates were not saving correctly.
Ah! Using just one date picker, it shows the correct date and year. But how to have the two fields show their own dates/year?
 
10:50 AM
As I said, that is a completely different problem. I would suggest that you rewrite the question accordingly.
 
It is but I've figured it out ;)
 

last day (14 days later) »