<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>[iPhoneiOS dev:tips]; &#187; Date and Time</title>
	<atom:link href="http://iPhoneDeveloperTips.com/category/date-and-time/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com</link>
	<description>Tips and Tricks for iPhone developers</description>
	<lastBuildDate>Thu, 29 Jul 2010 17:59:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Date Formatter Examples &#8211; Take 4: Setting Locale</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/date-formatter-examples-take-4-setting-locale.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/date-formatter-examples-take-4-setting-locale.html#comments</comments>
		<pubDate>Tue, 07 Jul 2009 13:20:11 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Date and Time]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=2531</guid>
		<description><![CDATA[A reader recently wrote and asked how to show a date output in a different language. By default, the NSDateFormatter will use the locale set on the device, so no code specific locale changes are required if you want your app to display in the current locale. However, if you need to display a date [...]]]></description>
			<content:encoded><![CDATA[<p>A reader recently wrote and asked how to show a date output in a different language. By default, the NSDateFormatter will use the locale set on the device, so no code specific locale changes are required if you want your app to display in the current locale. </p>
<p>However, if you need to display a date in a locale other than the current setting on the device, here&#8217;s how to do it:<br />
<span id="more-2531"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Create date object  </span>
<span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>date <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Create date formatter</span>
<span style="color: #400080;">NSDateFormatter</span> <span style="color: #002200;">*</span>dateFormat <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDateFormatter</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>dateFormat setDateFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;EEEE MMMM d, YYYY&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Set the locale as needed in the formatter (this example uses Japanese)</span>
<span style="color: #002200;">&#91;</span>dateFormat setLocale<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSLocale</span> alloc<span style="color: #002200;">&#93;</span> initWithLocaleIdentifier<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ja_JP&quot;</span><span style="color: #002200;">&#93;</span>
    autorelease<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Create date string from formatter, using the current date</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>dateString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>dateFormat stringFromDate<span style="color: #002200;">:</span>date<span style="color: #002200;">&#93;</span>;  
&nbsp;
<span style="color: #11740a; font-style: italic;">// We're done with this now</span>
<span style="color: #002200;">&#91;</span>dateFormat release<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Show that the locale on the device is still set to US </span>
<span style="color: #400080;">NSLocale</span> <span style="color: #002200;">*</span>locale <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSLocale</span> currentLocale<span style="color: #002200;">&#93;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Current Locale: %@&quot;</span>, <span style="color: #002200;">&#91;</span>locale localeIdentifier<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Display the date string in format we set above</span>
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Date: %@:&quot;</span>, dateString<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The output from the code above looks as follows:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/07/locale.png" /></p>
<p><strong>More on Date Formatting:</strong></p>
<p><a href="http://iphonedevelopertips.com/cocoa/date-formatter-examples.html">Date Formatter Examples &#8211; Take 1: NSDateFormatter</a><br />
<a href="http://iphonedevelopertips.com/cocoa/date-formatters-examples-take-2.html">Date Formatter Examples &#8211; Take 2: Format Strings</a><br />
<a href="http://iphonedevelopertips.com/cocoa/date-formatters-examples-take-3.html">Date Formatter Examples &#8211; Take 3: Date from String</a></p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/cocoa/date-formatter-examples-take-4-setting-locale.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Determining Elapsed Time</title>
		<link>http://iPhoneDeveloperTips.com/date-and-time/determining-elapsed-time.html</link>
		<comments>http://iPhoneDeveloperTips.com/date-and-time/determining-elapsed-time.html#comments</comments>
		<pubDate>Wed, 06 May 2009 03:22:08 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Date and Time]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=2202</guid>
		<description><![CDATA[I recently wrote a short block of code to display an activity indicator while waiting for an image to be written to the iPhone camera roll. The problem I ran into is that the time to write the image file varies, depending on the image size. In some cases the activity indicator would be visible [...]]]></description>
			<content:encoded><![CDATA[<p>I recently wrote a short block of code to display an activity indicator while waiting for an image to be written to the iPhone camera roll. The problem I ran into is that the time to write the image file varies, depending on the image size. In some cases the activity indicator would be visible for a few seconds, other times the activity indicator flashed on/off screen so quickly, if you blinked, you&#8217;d miss it.</p>
<p>I needed a way to display the activity indicator a consistent amount of time for each file save, regardless of how long it took to write the file. Here&#8217;s the workaround I came up with &#8211; I began by saving the start time and creating an activity indicator:<br />
<span id="more-2202"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">  <span style="color: #11740a; font-style: italic;">// When did we start the image save ?</span>
  <span style="color: #11740a; font-style: italic;">// CFTimeInterval is a double (floating point number)</span>
  CFTimeInterval startTime <span style="color: #002200;">=</span> CFAbsoluteTimeGetCurrent<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;    
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Start the activity indicator</span>
  UIActivityIndicatorView activityIndicator <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIActivityIndicatorView alloc<span style="color: #002200;">&#93;</span>
                    <span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">400</span>, <span style="color: #2400d9;">30</span>, <span style="color: #2400d9;">30</span><span style="color: #002200;">&#93;</span>;
  activityIndicator.activityIndicatorViewStyle <span style="color: #002200;">=</span> UIActivityIndicatorViewStyleGray;
  activityIndicator.autoresizingMask <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>UIViewAutoresizingFlexibleLeftMargin |
                    UIViewAutoresizingFlexibleRightMargin |
                    UIViewAutoresizingFlexibleTopMargin |
                    UIViewAutoresizingFlexibleBottomMargin<span style="color: #002200;">&#41;</span>;
  <span style="color: #002200;">&#91;</span>activityIndicator sizeToFit<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>activityIndicator<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>activityIndicator startAnimating<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Next, I wrote the image to the camera roll and then checked for the amount of time elapsed:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">&nbsp;
  <span style="color: #11740a; font-style: italic;">// Save image code here...</span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">// How much time has elapsed ?</span>
  CFTimeInterval difference <span style="color: #002200;">=</span> CFAbsoluteTimeGetCurrent<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">-</span> startTime;</pre></div></div>

<p>The last check I needed was to see if the amount of time that has elapsed was greater or less than the minimum time I wanted to display the activity indicator. If less than the desired amount of time, sleep for the amount of time needed to hit the minimum display time.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">  <span style="color: #6e371a;">#define SECONDS_TO_DISPLAY_ACTIVITY_INDICATOR 3</span>
&nbsp;
  ...
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Determine if some predetermined amount of time has passed.  </span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>difference &lt; SECONDS_TO_DISPLAY_ACTIVITY_INDICATOR<span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> sleepForTimeInterval<span style="color: #002200;">:</span>SECONDS_TO_DISPLAY_SAVING_MSG <span style="color: #002200;">-</span> difference<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Remove the activity indicator and message</span>
  <span style="color: #002200;">&#91;</span>activityIndicator stopAnimating<span style="color: #002200;">&#93;</span>;  
  <span style="color: #002200;">&#91;</span>activityIndicator removeFromSuperview<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>activityIndicator release<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>For my needs accuracy was not key. So even though CFTimeInterval is a double, I simply allow any rounding up or down to take place.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/date-and-time/determining-elapsed-time.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Date Formatter Examples &#8211; Take 3: Date from String</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/date-formatters-examples-take-3.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/date-formatters-examples-take-3.html#comments</comments>
		<pubDate>Wed, 26 Nov 2008 08:34:37 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Date and Time]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1072</guid>
		<description><![CDATA[While working on an iPhone application recently, I needed to convert a date read from an XML stream that was in the following format: 20081122 to a nicely formatted string for display on the device: Saturday November 22, 2008. How to get there from here is now obvious, however, when I first encountered this dilemma [...]]]></description>
			<content:encoded><![CDATA[<p>While working on an iPhone application recently, I needed to convert a date read from an XML stream that was in the following format: 20081122 to a nicely formatted string for display on the device: Saturday November 22, 2008.<br />
<span id="more-1072"></span></p>
<p>How to get there from here is now obvious, however, when I first encountered this dilemma the solution wasn&#8217;t apparent. The reason being, there is significant depth in the Cocoa frameworks and half the battle in becoming proficient as an iPhone developer is to have an opportunity to explore the range of APIs. Albeit the solution was right under my nose the whole time, my first pass was to take a more traditional route of trying to parse the string and rebuild a more &#8220;traditional&#8221; date format which I could use to create a date object. So, skipping all that, here&#8217;s the proper solution&#8230;</p>
<p>If you&#8217;ve ever worked with dates in Cocoa, chances are you are familiar with the <em>stringFromDate</em> method of the <em>NSDateFormatter</em>. For example, the code below will convert the current date to a string that looks like this: Wednesday November 26, 2008<br />
<!--more--></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>date <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSDateFormatter</span> <span style="color: #002200;">*</span>dateFormat <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDateFormatter</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>dateFormat setDateFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;EEEE MMMM d, YYYY&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>dateString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>dateFormat stringFromDate<span style="color: #002200;">:</span>date<span style="color: #002200;">&#93;</span>;  
<span style="color: #002200;">&#91;</span>dateFormat release<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>The trick is two-fold, the date format string to specify desired output, and the method <em>stringFromDate</em> to convert the date object to an NSString.</p>
<p>I&#8217;m sure you can see where I&#8217;m going with this&#8230;the solution I was looking for to convert a date (stored as a string) that was in a pre-defined format (i.e. 20081122) to a date object is as simple as using the method <em>dateFromString</em>. The primary difference is that the format string needs to represent the current format of the date that is to be read (versus the desired output format).</p>
<p>The code below converts a string that represents a date to an NSString object, with the output as follows: Saturday November 22, 2008:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>dateStr <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;20081122&quot;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Convert string to date object</span>
<span style="color: #400080;">NSDateFormatter</span> <span style="color: #002200;">*</span>dateFormat <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDateFormatter</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>dateFormat setDateFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;yyyyMMdd&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>date <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>dateFormat dateFromString<span style="color: #002200;">:</span>dateStr<span style="color: #002200;">&#93;</span>;  
&nbsp;
<span style="color: #11740a; font-style: italic;">// Convert date object to desired output format</span>
<span style="color: #002200;">&#91;</span>dateFormat setDateFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;EEEE MMMM d, YYYY&quot;</span><span style="color: #002200;">&#93;</span>;
dateStr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>dateFormat stringFromDate<span style="color: #002200;">:</span>date<span style="color: #002200;">&#93;</span>;  
<span style="color: #002200;">&#91;</span>dateFormat release<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p><strong>More on Date Formatting:</strong></p>
<p><a href="http://iphonedevelopertips.com/cocoa/date-formatter-examples.html">Date Formatter Examples &#8211; Take 1: NSDateFormatter</a><br />
<a href="http://iphonedevelopertips.com/cocoa/date-formatters-examples-take-2.html">Date Formatter Examples &#8211; Take 2: Format Strings</a><br />
<a href="http://iphonedevelopertips.com/cocoa/date-formatter-examples-take-4-setting-locale.html">Date Formatter Examples &#8211; Take 4: Setting Locale</a></p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/cocoa/date-formatters-examples-take-3.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Date Formatter Examples &#8211; Take 2: Format Strings</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/date-formatters-examples-take-2.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/date-formatters-examples-take-2.html#comments</comments>
		<pubDate>Mon, 10 Nov 2008 09:06:34 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Date and Time]]></category>
		<category><![CDATA[date formatter]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1019</guid>
		<description><![CDATA[In the first post on working with dates several of the examples use the &#8220;old style&#8221; date format syntax. The examples work, however, I want to show an additional example that uses the ICU (International Components for Unicode) library for format strings. Here is a short list of sample formats using ICU: The format specifiers [...]]]></description>
			<content:encoded><![CDATA[<p>In the first post on <a href="http://iphonedevelopertips.com/cocoa/date-formatter-examples.html" target="_blank">working with dates</a> several of the examples use the &#8220;old style&#8221; date format syntax. The examples work, however, I want to show an additional example that uses the <a href="http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns" target="_blank">ICU</a> (International Components for Unicode) library for format strings.<br />
<span id="more-1019"></span></p>
<p>Here is a short list of sample formats using ICU:</p>
<p><img width="450" src="http://iPhoneDeveloperTips.com/wp-content/uploads/2008/11/dateformatter.png"/></p>
<p>The format specifiers are quite straightforward, Y = year, M = month, etc. Changing the number of specifiers for a field, changes the output. For example, MMMM generates the full month name &#8220;November&#8221;, MMM results in &#8220;Nov&#8221; and MM outputs &#8220;11&#8243;.</p>
<p>What follows is an example to create the following date string:</p>
<p><em><strong>Saturday November 8, 2008</strong></em>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>today <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSDateFormatter</span> <span style="color: #002200;">*</span>dateFormat <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDateFormatter</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>dateFormat setDateFormat<span style="color: #002200;">:</span>@<span style="color: #002200;">&amp;</span>quot;EEEE MMMM d, YYYY<span style="color: #002200;">&amp;</span>quot;<span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>dateString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>dateFormat stringFromDate<span style="color: #002200;">:</span>today<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>dateFormat release<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Here&#8217;s another example showing the current time:</p>
<p><em><strong>9:20 AM, PST</strong></em>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>today <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span>; 
<span style="color: #400080;">NSDateFormatter</span> <span style="color: #002200;">*</span>dateFormat <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDateFormatter</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>dateFormat setDateFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;h:mm a, zzz&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>dateString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>dateFormat stringFromDate<span style="color: #002200;">:</span>today<span style="color: #002200;">&#93;</span>;  
<span style="color: #002200;">&#91;</span>dateFormat release<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p><strong>More on Date Formatting:</strong></p>
<p><a href="http://iphonedevelopertips.com/cocoa/date-formatter-examples.html">Date Formatter Examples &#8211; Take 1: NSDateFormatter</a><br />
<a href="http://iphonedevelopertips.com/cocoa/date-formatters-examples-take-3.html">Date Formatter Examples &#8211; Take 3: Date from String</a><br />
<a href="http://iphonedevelopertips.com/cocoa/date-formatter-examples-take-4-setting-locale.html">Date Formatter Examples &#8211; Take 4: Setting Locale</a></p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/cocoa/date-formatters-examples-take-2.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Date Formatter Examples &#8211; Take 1: NSDateFormatter</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/date-formatter-examples.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/date-formatter-examples.html#comments</comments>
		<pubDate>Thu, 30 Oct 2008 12:24:23 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Date and Time]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=933</guid>
		<description><![CDATA[Sometimes all you&#8217;re really looking for is a basic chunk of code to get something done. For example, I was working on an application yesterday and needed to display the current date in text format: October 29, 2008. A simple concept for sure, however, with the many nuances of date formatters, it takes some time [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes all you&#8217;re really looking for is a basic chunk of code to get something done. For example, I was working on an application yesterday and needed to display the current date in text format: October 29, 2008. A simple concept for sure, however, with the many nuances of date formatters, it takes some time to pull together the &#8220;right&#8221; code.<br />
<span id="more-933"></span></p>
<p>So, to save you some time, here are several simple examples for displaying date information:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"> <span style="color: #11740a; font-style: italic;">// &lt;strong&gt;Output -&gt;  Date: 10/29/08&lt;/strong&gt;</span>
 <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>today <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> dateWithTimeIntervalSinceNow<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
 <span style="color: #400080;">NSDateFormatter</span> <span style="color: #002200;">*</span>dateFormat <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDateFormatter</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
 <span style="color: #002200;">&#91;</span>dateFormat setDateStyle<span style="color: #002200;">:</span>NSDateFormatterShortStyle<span style="color: #002200;">&#93;</span>;
 <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>dateString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>dateFormat stringFromDate<span style="color: #002200;">:</span>today<span style="color: #002200;">&#93;</span>;
 NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Date: %@&quot;</span>, dateString<span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p>Notice above how the style of the output is set using <em>NSDateFormatterShortStyle</em>. There are additional canned formats as well such as <em>NSDateFormatterFullStyle</em> and <em>NSDateFormatterNoStyle</em>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"> <span style="color: #11740a; font-style: italic;">// &lt;strong&gt;Output -&gt;  Date: 10/29/2008 08:29PM&lt;/strong&gt;    </span>
 <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>today <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span>;
 <span style="color: #400080;">NSDateFormatter</span> <span style="color: #002200;">*</span>dateFormat <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDateFormatter</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
 <span style="color: #002200;">&#91;</span>dateFormat setDateFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;MM/dd/yyyy hh:mma&quot;</span><span style="color: #002200;">&#93;</span>;
 <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>dateString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>dateFormat stringFromDate<span style="color: #002200;">:</span>today<span style="color: #002200;">&#93;</span>;
 NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;date: %@&quot;</span>, dateString<span style="color: #002200;">&#41;</span>;
 <span style="color: #002200;">&#91;</span>dateFormat release<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>The example above shows how you can manage memory without using autorelease. As suggested in the comment below (thanks Nick) this is generally a preferred approach on the iPhone where applicable.</p>
<p>These examples merely skim the surface of what you can do when working with dates. Look at the documentation for specifics on how to tweak the specifier strings to create variations of the date output.</p>
<p><strong>More on Date Formatting:</strong></p>
<p><a href="http://iphonedevelopertips.com/cocoa/date-formatters-examples-take-2.html">Date Formatter Examples &#8211; Take 2: Format Strings</a><br />
<a href="http://iphonedevelopertips.com/cocoa/date-formatters-examples-take-3.html">Date Formatter Examples &#8211; Take 3: Date from String</a><br />
<a href="http://iphonedevelopertips.com/cocoa/date-formatter-examples-take-4-setting-locale.html">Date Formatter Examples &#8211; Take 4: Setting Locale</a></p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/cocoa/date-formatter-examples.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
