I have a String
in this format: "2019-03-11T17:04:00+0100". I need to convert that string to the one that will be in this format: "03.11 17:04". I already tried some suggestions for instance this one.

- 5,383
- 7
- 50
- 89

- 37
- 8
-
[Are you sure you want to do it on `String` representation level? Because it looks like it's date string formatting issue.](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) Do you have the date as `Date` object? – user28434'mstep Mar 11 '19 at 14:53
-
2Regex is not the way to go here. The input you specified is a POSIX datetime string and can be read into `DateFormatter` to decuce a `Date` object. This could then be fed into another `DateFormatter` with the format you want specified to get the output. Far more reliable than RegEx. – Jacob King Mar 11 '19 at 14:54
2 Answers
As per my comment, this is a task for DateFormatter
rather than RegeX. I threw this together in a playground quickly to demonstrate what I mean.
let inFormatter = DateFormatter()
inFormatter.locale = Locale(identifier: "en_US_POSIX")
inFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
let input = "2019-03-11T17:04:00+0100"
let dateFromInput = inFormatter.date(from: input)! // This should be unwrapped properly in your code.
let outFormatter = DateFormatter()
outFormatter.locale = Locale(identifier: "en_US_POSIX")
outFormatter.dateFormat = "MM. dd HH:mm"
let output = outFormatter.string(from: dateFromInput)
print(output) // Prints 03. 11 16:04.
The premise is that you provide a format for which to parse the input string against, this is transcoded to a Date
object which you can then transcode to your desired output format with a second DateFormatter
.
EDIT:
As pointed out by @user28434, the input you are passing in looks like CET (Central European Time); When I configure the output DateFormatter
, I do not specify a time zone so it defaults to my local time zone, GMT (Greenwich Mean Time). This would obviously cause the output to be different based on the location of the user in the world, which should be expected/desired. But it's worth highlighting. You can use outFormatter.timeZone = TimeZone(identifier: "CET")
to force a CET output.

- 6,025
- 4
- 27
- 45
-
1
-
1Indeed it should, though, this doesn't seem to effect the output which begs the question as to whether specifying a locale is necessary. I would still do so, to play it safe in case your user has a non-standard system locale set. – Jacob King Mar 11 '19 at 15:05
-
2It is always necessary to specify "en_US_POSIX" when parsing fixed date formats. Note that `ZZZZZ` takes care of the timezone `+0100`. – Leo Dabus Mar 11 '19 at 15:06
-
-
It looks like question had `input date string` and `output date string` in the same time zone, while this answer uses current client timezone for `output` (`17:04` in the question, `16:04` in the answer). – user28434'mstep Mar 11 '19 at 15:30
-
1Well spotted; that would make sense given the OP seems to be located in CET whereas I am GMT. I'll make an amendment with this in mind. – Jacob King Mar 11 '19 at 15:37
You can use DateFormatter instead of regex, first, convert the given string to a date with the string format, then convert the resulted date to a string with the desired format.
func convertISO8601DateStringToDate(dateStr: String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
return dateFormatter.date(from: dateStr)
}
func convertDateToReadableOutput(date: Date) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM.dd HH:mm"
return dateFormatter.string(from: date)
}
you can use these two methods as below:
if let date = stringToDateConverter(dateStr: "2019-03-11T17:04:00+0100") {
print(dateToStringConverter(date: date))
}

- 146
- 6
-
1Your method names are ambiguous here. Elsewhere in the code, it is totally unclear as to what each method does. You have followed the same convention but the methods actually do different things. These would be better as `convertISO8601DateStringToDate:` and `convertDateToReadableOutput` or something similar. – Jacob King Mar 11 '19 at 15:07