D
D
DeNn2011-01-14 13:55:43
Cocoa Touch
DeNn, 2011-01-14 13:55:43

NSDate in iOS 4.2?

I noticed that [NSDate date] returns the date without regard to the time zone. For example, I have ("Europe / Kiev")
At 14:00 hours, in the log 12:00
I used this crutch:

<font color="black"><ol>
<li>&nbsp;</li>
<li><font color="#002200">+</font> <font color="#002200">(</font><font color="#400080">NSDate</font><font color="#002200">*</font><font color="#002200">)</font>currentDate <font color="#002200">{</font>&nbsp; &nbsp; &nbsp; &nbsp; </li>
<li>&nbsp; &nbsp; &nbsp; &nbsp; <font color="#11740a"><i>// Get conversion to months, days, hours, minutes</i></font></li>
<li>&nbsp; &nbsp; &nbsp; &nbsp; <font color="#a61390">unsigned</font> <font color="#a61390">int</font> unitFlags <font color="#002200">=</font> NSYearCalendarUnit | NSMonthCalendarUnit | &nbsp;NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;</li>
<li>&nbsp; &nbsp; &nbsp; &nbsp; <font color="#400080">NSDateComponents</font> <font color="#002200">*</font>dateComponents <font color="#002200">=</font> <font color="#002200">[</font><font color="#002200">[</font><font color="#400080">NSCalendar</font> currentCalendar<font color="#002200">]</font> components<font color="#002200">:</font>unitFlags fromDate<font color="#002200">:</font>self<font color="#002200">]</font>;</li>
<li>&nbsp; &nbsp; &nbsp; &nbsp; <font color="#002200">[</font>dateComponents setTimeZone<font color="#002200">:</font><font color="#002200">[</font><font color="#400080">NSTimeZone</font> timeZoneWithAbbreviation<font color="#002200">:</font><font color="#bf1d1a">@</font><font color="#bf1d1a">&quot;GMT&quot;</font><font color="#002200">]</font><font color="#002200">]</font>;</li>
<li>&nbsp; &nbsp; &nbsp; &nbsp; <font color="#400080">NSDate</font> <font color="#002200">*</font>date <font color="#002200">=</font> <font color="#002200">[</font><font color="#002200">[</font><font color="#400080">NSCalendar</font> currentCalendar<font color="#002200">]</font> dateFromComponents<font color="#002200">:</font>dateComponents<font color="#002200">]</font>;</li>
<li>&nbsp; &nbsp; &nbsp; &nbsp; <font color="#a61390">return</font> date;</li>
<li><font color="#002200">}</font></li>
<li>&nbsp;</li>
</ol></font>

Maybe there is a more elegant solution?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question