DateFormatter doesn't return date for HH:mm:ss
From Technical Q&A QA1480 – NSDateFormatter and Internet Dates (emphasis added):
On the other hand, if you're working with fixed-format dates, you should first set the locale of the date formatter to something appropriate for your fixed format. In most cases the best locale to choose is "en_US_POSIX", a locale that's specifically designed to yield US English results regardless of both user and system preferences.
This will prevent the date from being interpreted according to
the user's regional settings:
let dateFormatter = DateFormatter()
// Set the locale first ...
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
// ... and then the date format:
dateFormatter.dateFormat = "HH:mm:ss"
// ...
See also What is the best way to deal with the NSDateFormatter locale "feechur"?.
Date format ( yyyy-MM-dd HH:mm:ss VV) returns nil
You should always set the locale
of your DateFormatter
when dealing with hardcoded Date formats. You can use en_US_POSIX
in most scenarios.
let dateString = "2016-01-21 00:29:09 Etc/GMT"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss VV"
let dateObject = dateFormatter.date(from: dateString)
print(dateObject)
Swift `yyyy-MM-dd'T'HH:mm:ss.sssZ` string to date
Your format is wrong. It should be:
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
Have a look at date time format here:
https://unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns
Issue in formatting Date in swift 4
HH:mm:ss
gives you the time in 24 hour format, so a
doesn't make sense inside the format. Or you can use hh:mm:ss a
.
Date()
gives you the time in UTC
. You need to provide the timezone information to get the date in your timezone.
let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MMM-yyyy HH:mm:ss"
dateFormatter.timeZone = .current
let localDate = dateFormatter.string(from: date)
print(localDate)
localDate
will give you the date in your current timezone.
DateFormatter date from string returns nil when iPhone Date & Time 24-Hour Time is off
You're using a very standardized timestamp format, which allows you to take advantage of the ISO8601DateFormatter
.
let dateString = "2020-03-05T09:00:00+00:00"
let df = ISO8601DateFormatter()
df.formatOptions = [.withInternetDateTime]
if let date = df.date(from: dateString) {
print(date) // 2020-03-05 09:00:00 +0000
}
If a machine (like your server) is generating the timestamp then it will (should) always be in zulu time (GMT) so you don't need to do anything beyond this. You could specify a time zone but there isn't a point since the string will always zero it out for you.
df.timeZone = TimeZone(secondsFromGMT: 0)
This string represents an absolute moment in time. If you need a relative moment in time, such as the local time from the source, you'll need to identify that time zone and apply it here, which is also very straighforward.
Swift date from string conversion doesn't work but the format works with online service?
Small tip when you are working with DateFormatter
, if it doesn't work one way, then do it the other way:
let string = dateFormatter.string(from: Date())
print("string: \(string)")
This way you'll see how your format is really interpreted.
If you do so, you'll get:
$> string: Fri, Aug 03, 2018, 12:05:09 GMT
See the extras ,
and the Aug 03
vs 03 Aug
?
Now, your issue is that you misunderstood localizedDateFormatFromTemplate
.
In fact you could remove spaces, punctuations and order.
For instance, dateFormatter.setLocalizedDateFormatFromTemplate("ddyyyyHHmmsszEMMM")
gives the same output.
You just needs to give it what kind of infos you want (day, year, months, etc.), and it create the correct dateFormat
corresponding in the localized version (adding then extra punctuations, etc.).
So use instead:
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss z"
Side note:
I currently live in GMT+2, so I added dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
to make it work.
Related Topics
Converting Nsstring to Nsdate (And Back Again)
Undefined Symbols For Architecture Arm64
Status Bar and Navigation Bar Issue in Ios7
Periodic iOS Background Location Updates
Starting Iphone App Development in Linux
Ios 7: Uitableview Shows Under Status Bar
How to Detect Avplayer and Get Url of Current Video from Wkwebview
Transport Security Has Blocked a Cleartext Http
Attempt to Present Uiviewcontroller on Uiviewcontroller Whose View Is Not in the Window Hierarchy
Nsurlsession/Nsurlconnection Http Load Failed on iOS 9
Remove HTML Tags from an Nsstring on the Iphone
Get to Uiviewcontroller from Uiview
Detect If the Device Is Iphone X