NSDateFormatter for datetime
try
"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
The Date Format Patterns guide suggests that "S" is the format specifier for fractions of seconds.
Date Format in Swift
You have to declare 2 different NSDateFormatters
, the first to convert the string to a NSDate
and the second to print the date in your format.
Try this code:
let dateFormatterGet = NSDateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateFormatterPrint = NSDateFormatter()
dateFormatterPrint.dateFormat = "MMM dd,yyyy"
let date: NSDate? = dateFormatterGet.dateFromString("2016-02-29 12:24:26")
print(dateFormatterPrint.stringFromDate(date!))
Swift 3 and higher:
From Swift 3 NSDate
class has been changed to Date
and NSDateFormatter
to DateFormatter
.
let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "MMM dd,yyyy"
if let date = dateFormatterGet.date(from: "2016-02-29 12:24:26") {
print(dateFormatterPrint.string(from: date))
} else {
print("There was an error decoding the string")
}
How to convert datetime string into NSDate in iOS?
Try to set like this
NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:currDate];
How to handle different date time formats using NSDateFormatter
A date formatter can only handle one format at a time. You need to take this approach:
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [f dateFromString:@"2010-01-10 13:55:15"];
NSDateFormatter *f2 = [[NSDateFormatter alloc] init];
[f2 setDateFormat:@"d. MMMM YYYY"];
NSString *s = [f2 stringFromDate:date];
s will now be "10. January 2010"
Set current time to yyyy-MM-dd 00:00:00 using Swift
let dateString = "2015-08-12 09:30:41 +0000"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
if let dateFromString = dateFormatter.dateFromString(dateString.componentsSeparatedByString(" ").first ?? "") {
println(dateFromString) // "2015-08-12 00:00:00 +0000"
}
Swift 4
let dateString = "2019-05-04 09:30:41 +0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.calendar = Calendar(identifier: .gregorian)
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
if let dateFromString = dateFormatter.date(from: dateString.components(separatedBy: " ").first ?? "") {
print(dateFromString) // "2019-05-04 00:00:00 +0000"
}
Related Topics
How to Center a Label Horizontally for All iOS Devices in Swift
Swiftui: How to Change the Tint Color (Background Color) of a Navigationview
Uitextfield Doesn't End Editing When Button Clicked( Delegate Textfielddidendediting )
iOS Swift - Custom Camera Overlay
How to Get All Keys and Values into Separate String Arrays from Nsdictionary in Swift
Compiler Error When Assigning the Delegate for a Protocol in Swift iOS
How to Apply Multiple Masks to Uiview
Swiftui - Optional Timer, Reset and Recreate
-[Uithreadsafenode Canperformaction:Withsender:]: Unrecognized Selector Sent to Instance
Swift, Google Map Fit Bound for All the Markers
How to Access Index of Tabbar Item Using Swift
Cannot Set Color of Button's Label Inside Menu in Swiftui
Skspritenode Subclassing Swift
Alamofireimage Disk Cache Not Working