Tuesday, May 31, 2016

How to convert NSString to NSDate in Objective C

Sometimes you need to convert a date string to date (NSString to NSDate). Suppose from server you are getting a date string with its date format. But in your app you need to convert it in date to change the format or to
change the time zone. It is easy to change the date format or zone information than a string. Let's see how can we do this date conversion in Objective C easily.
For doing this NSString to NSDate conversion you need to know just one information, which is the date format of the date string. 

Suppose you are getting a date string like "2016.12.31T12:05Z" and format is "yyyy.MM.dd'T'HH:mmZ"
In XCode write as follows

NSString *utcDateString=@"2016.12.31T12:05Z";
NSDateFormatter* utcFormatter = [[NSDateFormatter alloc] init];
[utcFormatter setDateFormat:@"yyyy.MM.dd'T'HH:mmZ"];
NSDate *utcDate=[[NSDate alloc]init];
utcDate=[utcFormatter dateFromString:utcDateString];
NSLog(@"Given date In NSDate Format: %@",utcDate);

Output is : Given date In NSDate Format: 2016-12-31 12:05:00 +0000

Please be careful with the date format. If you use wrong date format you will get null (Image 1)

Image 1: Returned null value for date time


What if you need to convert the NSDate to NSString?? For this date and string conversion you need to be sure about two things. One is obviously the date format, another one is the time zone name.

To convert NSDate to NSString write :
NSDateFormatter* localDate = [[NSDateFormatter alloc] init] ;
// [localDate setTimeZone:[NSTimeZone systemTimeZone]];
[localDate setDateFormat:@"MM/dd/yyyy"];
   
NSDateFormatter* localTime = [[NSDateFormatter alloc] init];
//[localTime setTimeZone:[NSTimeZone systemTimeZone]];
[localTime setDateFormat:@"hh:mm a"];
   
NSDateFormatter* utcDateTime = [[NSDateFormatter alloc] init] ;
[utcDateTime setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[utcDateTime setDateFormat:@"MM/dd/yyyy hh:mm a"];
   
NSString* localDateString = [localDate stringFromDate:utcDate];
NSString* localTimeString = [localTime stringFromDate:utcDate];
NSString* utcDateTimeString = [utcDateTime stringFromDate:utcDate];
   
NSLog(@"localDateString        :%@", localDateString);
NSLog(@"localTimeString       :%@", localTimeString);
NSLog(@"utcDateTimeString  :%@", utcDateTimeString);

Output is:
                 localDateString        :12-31-2016
                 localTimeString        :06:05 PM
                 utcDateTimeString   :12/31/2016 12:05 PM


By the default  time zone is set to your system time zone. So you do not need to set value of this instance to the date formatter. But if you set the system time zone value (systemTimeZone) will do no harm. Just set the timeZoneWithName instance value to get the date time of that specific zone. To get the zone name just run this line of code :  

                     NSLog(@"Time Zone Names%@", [NSTimeZone knownTimeZoneNames]);

3 comments:

  1. @"2019-12-27T06:41:10Z"

    How to convert into date

    ReplyDelete
    Replies
    1. for your date the date formatter will be like below. just replace the format with it.
      [utcFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];

      Delete