DateFormatter not working in ios By setting
I found the answer of this question. Sorry for the delay to post the answer. You need to enter NSLocale to the formatter as I have done in this example.
func dateFromstring(dateString:String)->NSDate {
//print(date)
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy'T'HH:mm:ssZZZ"
//DateFormatter To change By chakshu
var enUSPOSIXLocale:NSLocale=NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.locale=enUSPOSIXLocale
dateFormatter.timeZone = NSTimeZone(name: "GMT")
//dateFormatter.timeZone = NSTimeZone.localTimeZone()
var date = dateFormatter.dateFromString(dateString)
//print(date)
return date!
}
Hope this help.
Thanks
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)
}
}
swift NSDateFormatter not working
The 24-hour format is "HH", not "hh".
The reason that it works in the Playground may be
that user defined settings can override the 12/24-format choice, compare
What is the best way to deal with the NSDateFormatter locale "feechur"?.
To be on the safe side, set the "en_US_POSIX" locale for the date formatter:
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
// Swift 3:
formatter.locale = Locale(identifier: "en_US_POSIX")
Swift DateFormatter properties not changing
I think you misunderstand what setLocalizedDateFormatFromTemplate
does. Its documentation says:
Calling this method is equivalent to, but not necessarily implemented as, setting the
dateFormat
property to the result of calling thedateFormat(fromTemplate:options:locale:)
method, passing no options and the locale property value.
Now what does dateFormat(fromTemplate:options:locale:)
do? Lets see:
Return Value
A localized date format string representing the date format components given in template, arranged appropriately for the locale specified by locale.
The returned string may not contain exactly those components given in template, but may—for example—have locale-specific adjustments applied.
So dateFormat(fromTemplate:options:locale:)
tries to localise the template to a specified locale. If no locale is specified, it uses Locale.current
. For example:
// this produces "MM/dd/yyyy"
DateFormatter.dateFormat(fromTemplate: "yyyy dd MM", options: 0, locale: Locale(identifier: "en-US"))
That explains why it removes all the quoted strings in your format, because the localisation engine doesn't recognise your quoted strings, so to produce a "localised" version of your date format, the best it can do is to remove them. As far as it is concerned, the quoted strings could be in a different language!
So it's not that setLocalizedDateFormatFromTemplate
did not change dateFormat
. It did change it to "MMMM d, h:mm a"
, which is what iOS thinks is the best "localised" version of the format
"'Deliver on' MMMM d 'at' h:mm a 'sharp'"
In this situation, you should set dateFormat
directly, rather than setLocalizedDateFormatFromTemplate
, as you don't want a localised date format.
Swift 5 - Dateformatter not working as expected
Date
has no information about time zone, and default string representation is using a greenwich one. You can see it +0000
part in your string.
You can get description for your own time zone like this:
date.description(with: .current)
Related Topics
Storyboard Doesn't Contain a View Controller With Identifier
Iphone Get a List of All Ssids Without Private Library
Sorting Array Alphabetically With Number
How to Change the Device Orientation Programmatically in iOS 6
Iterate Over Snapshot Children in Firebase
Always Pass Weak Reference of Self into Block in Arc
Remove or Uninstall Library Previously Added: Cocoapods
Changing Tint/Background Color of Uitabbar
React Native: How to Select the Next Textinput After Pressing the "Next" Keyboard Button
How to Know When Uitableview Did Scroll to Bottom in Iphone
Dismiss Keyboard by Touching Background of Uitableview
Detect When an iOS App Is Launched For the First Time
How to Calculate the Size of a Folder
Create Uiimage With Solid Color in Swift
Ios: How to Share Text and Image on Social Networks
Reloaddata() of Uitableview With Dynamic Cell Heights Causes Jumpy Scrolling