Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 8. Dates and Times > Parsing Dates from Strings

Parsing Dates from Strings

From: stringdate.m

 6 NSDate *isoDate =
 7   [NSDate dateWithString:
 8     @"1982-06-15 06:10:00 +0000"];
 9
10 NSLocale *gb = [[NSLocale alloc]
11   initWithLocaleIdentifier:@"en_GB"];
12 NSDateFormatter *formatter =
13   [NSDateFormatter new];
14 [formatter setLocale: gb];
15 [formatter setDateStyle:
16   NSDateFormatterShortStyle];
17 [formatter setTimeStyle:
18   NSDateFormatterNoStyle];
19 NSDate *britishDate =
20   [formatter dateFromString:
21     @"15/06/1982"];

The NSDate class has a constructor that lets you construct dates from ISO 8601–formatted strings. This is useful when you are parsing dates from files used for interchange, but is not so useful when dealing with user-provided data.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint