I've the following code
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
dateFormatter.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
dateFormatter.stringFromDate(NSDate())
It produces date in the following format:
2015-08-06T15:44:49+0000
But I want it to be in the following format 2015-08-06T15:44:49+00:00
As you can see the offset needs to b +00:00 instead of +0000
You need to use "xxx" instead of "Z".
edit/update Swift 3 or later:
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.calendar = Calendar(identifier: .gregorian)
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssxxx"
dateFormatter.string(from: Date()) // "2019-03-22T14:36:07+00:00"
If you need some reference you can use this:
Related
The app crashes once the user selects any date of September.
It only happens with iOS 15.4.
I have datePicker formatted date to return always in UTC format en_GB.
#objc func doneDateClicked() {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .none //CHANGED 09.07.2020 date medium time none
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.timeZone = TimeZone(identifier: "GMT")
dateFormatter.locale = Locale(identifier: "en_GB")
dateFormatter.dateFormat = "dd MMM yyyy"
let timeFormatter = DateFormatter()
timeFormatter.dateStyle = .none
timeFormatter.timeStyle = .medium
timeFormatter.timeZone = TimeZone(secondsFromGMT: 0)
timeFormatter.timeZone = TimeZone(identifier: "GMT")
timeFormatter.locale = Locale(identifier: "en_150")
dateTextField.text = dateFormatter.string(from: (datePicker?.date )!)
self.view.endEditing(true)
}
Then I need to covert the selected date from string (dateTextField.text) to date
on iOS 15.4 the month September it shows "Sept".
code to convert from string to date
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_GB")
formatter.dateFormat = "dd MMM yyyy hh:mm:ss"
formatter.calendar = Calendar(identifier: .gregorian)
let datenN = formatter.date(from: "\(dateTextField.text ?? "") 00:00:00")
datenN crashing the app it returns nil
I tried to use code formatter.locale = Locale(identifier: "en_US_POSIX") but the app still crashing
Assuming this problem occurs just for September and on iOS 15.4, then
a quick work around, is to replace "Sept" with "Sep", using
dateTextField.text = dateFormatter.string(from: (datePicker?.date )!)
if dateTextField.text.contains("Sept") {
dateTextField.text = dateTextField.text.replacingOccurrences(of: "Sept", with: "Sep")
}
My date format in string is "2019-03-28".
My date formatter is like "yyyy-MM-dd"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00")
let assignDate = dateFormatter.date(from: deliveryDate)
What am I doing wrong here
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00")
let assignDate = dateFormatter.date(from: "2019-03-28")
print("assignDate \(assignDate)")
All works fine for me here!
assignDate Optional(2019-03-28 00:00:00 +0000)
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00")
let assignDate = dateFormatter.date(from: deliveryDate)
This is working fine
I cannot seem to figure out why only "24:00" returns nil instead of 12:00AM, whereas "23:59" returns correctly as 11:59PM.
let dateAsString = "24:00"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale
let date = dateFormatter.date(from: dateAsString)
dateFormatter.dateFormat = "h:mm a"
let Date12 = dateFormatter.string(from: date!)
print(Date12)
You are using the wrong date format for the hour component. For values from 1-24 you have to use k, if you have a leading zero you need to use kk:
let dateAsString = "24:00"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "kk:mm"
if let date = dateFormatter.date(from: dateAsString) {
dateFormatter.dateFormat = "h:mm a"
let date2 = dateFormatter.string(from: date)
print(date2) // "12:00 AM\n"
}
I'm trying to convert string to date, and again date to string.
I tried below, but it gives me a nil value error
let string = "2017-07-11T06:52:15.948Z"
let dateFormatter = DateFormatter()
let tempLocale = dateFormatter.locale // save locale temporarily
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date = dateFormatter.date(from: string)!
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
dateFormatter.locale = tempLocale // reset the locale
let dateString = dateFormatter.string(from: date)
print("EXACT_DATE : \(dateString)")
then I tried to use only the date without time by seperating the exact string like below
let string = "2017-07-11T06:52:15.948Z"
let dateonly = (string.components(separatedBy: "T"))[0]
let dateformatter = DateFormatter()
dateformatter.dateFormat = "yyyy-mm-dd"
dateformatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale!
let date = dateformatter.date(from: dateonly!)
print(date ?? "no date ")
dateformatter.dateFormat = "MMM d, yyyy" //Your New Date format as per requirement change it own
let newDate = dateformatter.string(from: date!) //pass Date here
print(newDate)
this works fine. but show me the wrong month
hope your help with this
for reference purpose I taken the answer from here for append the milliseconds SSS
just change the date format
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
to
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
for full code
let string = "2017-07-11T06:52:15.948Z"
let dateFormatter = DateFormatter()
let tempLocale = dateFormatter.locale // save locale temporarily
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date = dateFormatter.date(from: string)!
dateFormatter.dateFormat = "MMM d, yyyy" ; //"dd-MM-yyyy HH:mm:ss"
dateFormatter.locale = tempLocale // reset the locale --> but no need here
let dateString = dateFormatter.string(from: date)
print("EXACT_DATE : \(dateString)")
output
let dateFormatter = DateFormatter()
let dateFormatter2 = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date = dateFormatter.date(from: event!)
dateFormatter2.timeZone = TimeZone.current
let dateString = dateFormatter2.string(from: date!)
let finalDate = dateFormatter2.date(from: dateString)
return finalDate!
I have sports events that give me string dates in UTC which I am trying to convert into local time. This code is similar to the dozens of examples given in this site when asked this question, yet it doesn't work.
This is the iso8601 formatter :
static let iso8601: DateFormatter = {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
return formatter
}()
This turns your string into a Date.
You will need to add the time difference, most likely through TimeZone.autoupdatingCurrent