I am getting the following string from a server in my iOS app:
20140621-061250
How can I convert it to the local time?
How can I define my date formatter? Is this correct?
dateFormatter.dateFormat = @"YYYYMMd-HHmmss";
I am getting the following string from a server in my iOS app:
20140621-061250
How can I convert it to the local time?
How can I define my date formatter? Is this correct?
dateFormatter.dateFormat = @"YYYYMMd-HHmmss";
To get your string into a NSDate, you would use a NSDateFormatter like this:
You may want to read this post about working with Date and Time
EDIT:
To parse it as UTC you have to add the line:
Also, when you print it with NSLog, if you are using the same NSDateFormatter, you will get the input string as output (since you apply the inverse of the parsing function).
Here is the full code, for parsing and for getting the output with a standard format:
The question doesn't specify the nature of what you mean by converting, exactly, but the first thing you should do, regardless of the final goal, is to correctly parse the server response using a properly configured
NSDateFormatter
. This requires specification of the correct format string, and the time zone must be explicitly set on the formatter or it will infer it from the local time, which would be incorrect in most cases.Specify The Format String
Let's look at the input string provided:
This uses four digits for the year, two digits (with a zero-padding) for the month, and two digits (presumably, these will be zero-padded as well) for the day. This is followed by a
-
, then two digits to represent the hour, 2 digits for the minute, and 2 digits for the second.Referring to the Unicode date format standards, we can derive the format string in the following way. The four digits representing the calendar year will be replaced with
yyyy
in the format string. UseMM
for the month, anddd
for the day. Next would come the literal-
. For the hours, I assume that it will be in 24 hour format as otherwise this response is ambiguous, so we useHH
. Minutes are thenmm
and secondsss
. Concatenating the format specifiers yields the following format string, which we will use in the next step:In our program, this would look like:
Configure the input date formatter
The time format above does not specify a time zone, but because you have been provided the specification for the server response that it represents the UTC time, we can code this into our application. So, we instantiate an
NSDateFormatter
, set the correct time zone, and set the date format:Convert the input string to an
NSDate
For demonstration purposes, we hard-code the string you received from the server response; you would replace this definition of
inputString
with the one you get from the server:At this point, we have the necessary object to do any further conversions or calculations - an
NSDate
which represents the time communicated by the server. Remember, anNSDate
is just a time stamp - it has no relation to a time zone whatsoever, which only plays a role when converting to and from string representations of the date, or representations of a calendrical date viaNSDateComponents
.Next steps
The question doesn't clearly specify what type of conversion is needed, so we'll see an example of formatting the date to display in the same format as the server response (although, I can't think of a likely use case for this particular bit of code, to be honest). The steps are quite similar - we specify a format string, a time zone, configure a date formatter, and then generate a string (in the specified format) from the date:
Since I'm in UTC-06:00, printing
outputString
gives the following:It's likely you'll instead want to use
setDateStyle:
andsetTimeStyle:
instead of a format string if you're displaying this date to the user, or use anNSCalendar
to get anNSDateComponents
instance to do arithmetic or calculations on the date. An example for displaying a verbose date string to the user:Printing
outputString
here gives us the following:Note that setting the time zone appropriately will handle transitions over daylight savings time. Changing the input string to "20141121-061250" with the formatter style code above gives us the following date to display (Note that Mountain Standard Time is UTC-7):
Summary
Any time you get date input in a string form representing a calendar date and time, your first step is to convert it using an
NSDateFormatter
configured for the input's format, time zone, and possibly locale and calendar, depending on the source of the input and your requirements. This will yield anNSDate
which is an unambiguous representation of a moment in time. Following the creation of thatNSDate
, one can format it, style it, or convert it to date components as needed for your application requirements.One possible solution in Swift using NSDate extension (maybe it could help future viewers of this question):