This question already has answers here:
Instantiated optional variable shows as nil in Xcode debugger
(2 answers)
Weird behaviour of Xcode 11 Debugger - Showing values as nil when there's a value
(2 answers)
Closed 2 years ago.
I have a function like given below, and the date is displaying nil as in the screenshot, But while printing it shows the value. Did I miss something?
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = NSTimeZone(name: "UTC")! as TimeZone
dateFormatter.locale = Locale.init(identifier: "en_US_POSIX")
let date = dateFormatter.date(from: "2020-07-27 04:01:46")
Related
This question already has answers here:
Difference between 'YYYY' and 'yyyy' in NSDateFormatter
(4 answers)
Closed 2 years ago.
UIDatePicker shows one year ahead in the UITextfield.inputview when using "MM/dd/YYYY" format.
#objc public func dateChange(datePicker: UIDatePicker) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/YYYY"
dateOfBirthVC.customView.entryTextField.text = dateFormatter.string(from: datePicker.date)
}
Below is the code to track the changes in date picker and display back in the UITextField
datePicker?.addTarget(self, action: #selector(ViewController.dateChange(datePicker:)), for: .valueChanged)
dateOfBirthVC.customView.entryTextField.inputView = datePicker
Here is the problem. When user change the date in the date picker, the year in the UITextfield shows one year after. That happened only when user put the month for December and the date between 25 and 31.
Screenshot
change "MM/dd/YYYY"
to "MM/dd/yyyy"
This question already has an answer here:
swift NSDateFormatter not working
(1 answer)
Closed 5 years ago.
I am trying to fetch the currentTime in a 24 hour format with UTC timezone.
When I have debugged my app in different devices, I came across a strange problem.
The code works perfectly fine in my device, however it gives time in 12 hour format in my cousin's iPhone having iOS 11.2. So I have tried running app in another device which is iPhone X with same os.
I am not sure what went wrong but then I tried this in few other devices as well and the app runs fine in all other devices.
P.S. Older version of my app runs fine in cousin's phone too with the same code.
here is my code which I have used to get currentTime.
let dateFormatter = DateFormatter()
dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC") as TimeZone!
dateFormatter.dateFormat = "EEEE"
dayOfWeekString = dateFormatter.string(from: NSDate() as Date)
dateFormatter.dateFormat = "HH"
let CurrentTime = dateFormatter.string(from: NSDate() as Date)
let CHour = Int(CurrentTime)!
Looking for the solution
DateFormatter considers also the current locale.
It's recommended to set the locale to a fixed value
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")!
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "HH"
let currentTime = dateFormatter.string(from: Date())
However there is a more reliable way using Calendar. The hour date component is in 24 hour mode anyway and you don't need the conversion to Int
var calendar = Calendar(identifier: .gregorian)
calendar.timeZone = TimeZone(abbreviation: "UTC")!
let currentHour = calendar.component(.hour, from: Date())
Note: Don't use NSDate, NSLocale, NSTimeZone etc. in Swift. Use the native structs.
This question already has answers here:
Getting date from [NSDate date] off by a few hours
(3 answers)
Closed 5 years ago.
Please refer attached sample image in datepicker.
I already changed to today 2 26 PM
but you can notice debug mode still show 1 hour later than what I chose
2018-01-22 03:26:10 +0000
Any idea what it is?
Attached sample image.
You have to set the TimeZone for your DateFormatter like this.
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "GMT")// Set your time time zone here
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.locale = NSLocale.current
This question already has answers here:
Convert NSDate to NSString
(19 answers)
Closed 5 years ago.
Can anyone please help me converting current date to this format:
"2017-06-15T05:15:31.158Z".
I need this format only, another format is not acceptable to my backend.
Friend, please help me here.
try this
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let timeString = formatter.stringFromDate(NSDate())
print(timeString)
How to get yyyy-mm-dd hh:mm:ss format from "2015-06-26T00:10:00+01:00" in swift
This question already has an answer here:
DateFormatter doesn't return date for "HH:mm:ss"
(1 answer)
Closed 5 years ago.
Within our iphone app we parse a date gotten from an api call. The date returns correctly and is a valid date. Now only on some devices does it crash with the error of unexpectedly found nil while unwrapping an Optional value. Here is the code in question:
//formatDate(date: date, format: FullDateFormat)
class func formatDate(date: String, format: String)->String{
if date.characters.count == 0 {return "" }
let formatter = DateFormatter()
formatter.dateFormat = Constants.FullDateFormat
let nsDate = formatter.date(from: date)
formatter.dateFormat = format
return formatter.string(from: nsDate!)
}
nsDate is not being formatted as it is nil.
The Constants.FullDateFormat is a static string defined as "M/d/yyyy h:mm:ss a" as the date will always come in this format
The call to the class function will look like this
let newDate = Helpers.formatDate(date: "9/27/2017 9:26:51 AM", format: "h:mm a")
Some devices crash while majority don't. If we don't use the class function the app works correctly. I don't see any cause for it so if anyone sees why this may be happening and a possible solution, please let me know.
This may be a duplicate but didn't show up in any of the searches I performed. Thanks to community they pointed to another similar questions with answers already at stackoverflow. My apologies if this is a duplicate.
It is a matter of locales. DateFormatter is dependent on the device's current location settings, including date and time.
You can ensure that the formatter's locale is always static by setting its locale to en_US_POSIX:
formatter.locale = Locale(identifier: "en_US_POSIX")
See Apple's link for more details:
https://developer.apple.com/documentation/foundation/nsdateformatter