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
Related
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")
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 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
I am running the following code :
if let messagedate = curr_comment["timestamp"] as? String {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let date = dateFormatter.dateFromString(messagedate)
}
and here is the debugger output :
This started happening randomly today, I'm not sure why the date changes by 500 years through such a simple operation.
EDIT: This happens on a device that is using the Buddhist calendar.
It is because the device in question is not set for Gregorian calendar. It is using the Buddhist calendar. See Apple Technical Q&A 1480 which discusses the proper handling of date strings to gracefully handle international calendars.
If this yyyy-MM-dd HH:mm:ss format is for internal purposes (storing in database, communicating with web service, etc.), you should consider setting the locale of the formatter to en_US_POSIX. In Swift 3:
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
in Swift 2:
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
Frankly, this also begs the question as to what timezone the string representation of the date is using. You generally would want to make sure that date/time strings are consistently stored in UTC/GMT. In Swift 3:
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
Or, in Swift 2:
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
If you do that consistently throughout the app, you'll avoid weird timezone issues (e.g. if someone in NY posts something right now, I don't want it to tell me in CA that it happened three hours ago).
This question already has answers here:
Converting NSString to NSDate (and back again)
(17 answers)
Closed 7 years ago.
Could you please let me know How to convert the NSString of format (2015-04-30T21:53:11.0000) to NSDate? in Swift.
Use the dateFromString method in iOS' date formatter class.
There are a zillion examples and explanations just a google search away. Most will be in Objective-C, but that's very easy to convert to Swift.
Read this.
You would use NSDateFormatter for this.
let orig = "2015-04-30T21:53:11.0000"
let dsFormatter = NSDateFormatter()
dsFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSS"
let dsDate = dsFormatter.dateFromString(orig)