How to add characters into dateFormatter
I followed this answer, but it doesn’t work for me.
I want to format my time as:
let xFormatter = DateFormatter() xFormatter.dateFormat = "yyyy-MM-ddTHH:mm:ss-04:00" print(xFormatter.string(from: date))
I need a T between the date and time. however if I add that, my format turns into something like:
(The -4:00 at the end is a timeoffset which I hardcode…)
- Days between 2 NSDates in a calendar year swift
- Convert Date to Integer in Swift
- iOS Swift - Get the Current Local Time and Date Timestamp
- how to add Hours to date in swift in this format “2015-06-11T00:00:00.000Z” as in one string while parsing in SWIFT
- How to calculate the age based on NSDate
- Swift iOS doesRelativeDateFormatting have different values besides “Today” and “Yesterday”?
Solutions Collect From Internet About “How to add characters into dateFormatter”
Add single quotes
xFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss-04:00"
From the documentation:
… This includes the need to enclose ASCII letters in single quotes if they are intended to represent literal text.
Source: Unicode.org: Date Format Patterns
Be aware that the time zone is just an amendment to the string, it’s not considered by the date formatter.
To consider the time zone you have to set the
timeZone of the formatter
xFormatter.timeZone = TimeZone(secondsFromGMT: -14400)
In iOS 10.0+ and macOS 10.12+ there is a more convenient way to create an ISO8601 string
let isoFormatter = ISO8601DateFormatter() isoFormatter.timeZone = TimeZone(secondsFromGMT: -14400) isoFormatter.formatOptions = .withInternetDateTime print(isoFormatter.string(from: Date()))
- (String: AnyObject) does not have a member named 'subscript'
- Custom UIScrollView paging with scrollViewWillEndDragging
- Using Xcode for multiple developer IDs
- Why is iphone simulator bigger than the actual iphone?
- How do I include CSS resource in programmatically generated HTML for iPhone UIWebView?
- Loading UIViewController “from” Nib File
- Load ViewController Swift – Black Screen
- prepareForSegue destination controller property not being set
- AFNetworking Cause Error while using XCTest in Xcode5
- Create Tab bar controller and Navigation controller
- iOS add share extension on maps
- NSString to Emoji Unicode
- Objective-c search locations with radius
- Upgraded to iOS 7 beta / Xcode 5 beta and receiving warning “CODE_SIGN_ENTITLEMENTS specified without a valid Developer Signing Identity for iOS”
- Property vs. ivar in times of ARC