Swift: can't get NSDate dateFormatter right
It looks like your date formatter is a bit off. "MM'/'DD'/'YYYY"
reads "Padded month / day of the year (not month) / year of the current week (so Jan 1-6 could overlap and add side-effects)"
I'm guessing that what you're aiming for is "MM'/'dd'/'yyyy"
, which reads "Padded month / padded day of month / year"
For reference, here's the currently-used Unicode Technical Standard that Apple documents as their standard for iOS 7+ and OSX 10.9+ here
Can't find the right date formatter (DateFormatter) in iphone
In Swift 3
You can change your server time string to UTC time Date
as:
let dateFormatter: DateFormatter = DateFormatter()
var serverDateString = "2017-08-05T00:30:00.000+02:00"
let index = serverDateString.index(serverDateString.startIndex, offsetBy: 19)
serverDateString = serverDateString.substring(to: index)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = TimeZone.init(identifier: "UTC")
let currentDate: Date = dateFormatter.date(from: serverDateString)!
Can't Convert Date Format Into Display Date In Swift
You should always set your dateFormat locale to "en_US_POSIX"
before setting the dateFormat. Btw it looks like that your date string you are reading from database is always in UTC format, if that is the case you should set your dateFormatter timezone to zero secondsFromGMT and escape your date string timezone:
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSSIX")
dateFormatter.dateFormat = "EEE MMM dd yyyy HH:mm:ss 'GMT+0000 (Greenwich Mean Time)'"
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.date(from: "Thu Nov 12 2020 07:00:00 GMT+0000 (Greenwich Mean Time)")
You should also create a static date formatter to avoid creating a new one every time you call this method:
extension Formatter {
static let customDate: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSSIX")
dateFormatter.dateFormat = "EEE MMM dd yyyy HH:mm:ss 'GMT+0000 (Greenwich Mean Time)'"
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
return dateFormatter
}()
static let yyyyMMMd: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.setLocalizedDateFormatFromTemplate("yyyyMMMd")
return dateFormatter
}()
}
extension String {
func convertToDate() -> Date? {
Formatter.customDate.date(from: self)
}
func convertToDisplayFormat() -> String {
convertToDate()?.convertToMonthDayTimeFormat() ?? "N/A"
}
}
extension Date {
func convertToMonthDayTimeFormat() -> String {
Formatter.yyyyMMMd.string(from: self)
}
}
Playground testing:
"Thu Nov 12 2020 07:00:00 GMT+0000 (Greenwich Mean Time)".convertToDisplayFormat() // "Nov 12, 2020"
Swift dateformatter.date(from: string) strange behaviour
The problem is with the date format.
change these lines:
df.dateFormat="YYYY.MM.dd"
df.dateFormat="dd.MM.YYYY"
to this:
df.dateFormat="yyyy.MM.dd"
df.dateFormat="dd.MM.yyyy"
Date formatting not working in Swift 3
Date().toString() // convert date to string with userdefined format.
extension Date {
func toString() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM dd yyyy"
return dateFormatter.string(from: self)
}
}
DateFormatter. Strange behaviour on iOS 13.4.1 while 12 hour date style set in the iOS settings
The problem is fixed. Locale needs to be set.
formatter.locale = Locale(identifier: "en_US_POSIX")
Dateformatter issue in swift
With help of @Martin R, Following code worked for me.
let sourceDateString = "12/20/2016 11:45 AM"
let formatter = DateFormatter()
formatter.dateFormat = "MM/dd/yyyy hh:mm a"
formatter.timeZone = TimeZone(identifier: "UTC")
formatter.locale = Locale(identifier: "en_US_POSIX")
let date = formatter.date(from: sourceDateString)
print(date)
formatter.timeZone = NSTimeZone.local
formatter.dateFormat = "MMM dd,yyyy hh:mm a"
let strDate = formatter.string(from: date!)
print(strDate)
I forgot to add "POSIX".
Related Topics
Difference Between Dispatch_Async and Dispatch_Sync on Serial Queue
Creating a Uiimage from a Uicolor to Use as a Background Image for Uibutton
Add Entry to iOS .Plist File via Cordova Config.Xml
How to Create Negative Firebase Timestamp in Swift
Xcode Canvas for Swiftui Previews Does Not Show Up
Using Weak Self in Dispatch_Async Function
Uitableviewcell with Autolayout Left Margin Different on iPhone and iPad
Avcapturesession with Multiple Previews
How to Access Coredata Model in Today Extension (Ios)
After Upgrading to Xcode 9, Cordova App Won't Build, Error 70, Requires Provisioning Profile
How to Update a Localized Storyboard's Strings
iOS Static VS Dynamic Frameworks Clarifications
How to Turn a Cvpixelbuffer into a Uiimage
Duplicate Symbol Error in Nsmanagedobject Subclass
Dtassetproviderservice Could Not Start Dtxconnection with Simulator
What Should Xcode 6 Gitignore File Include