Converting NSDate

External developers... This forum's for you!

Converting NSDate

Postby Dave_Higgins » Sat Nov 05, 2011 5:21 am

Anybody got code examples of how to convert NSDate to something native to SC? Be it seconds, dateItems, long date/time, whichever.

I could take the time to figure it out, but I have a feeling I'd be spending a lot of time re-inventing a wheel that's be done several times over.

Thanks.
My two favorite teams are Detroit and whoever's playing Chicago.
User avatar
Dave_Higgins
 
Posts: 454
Joined: Mon Jul 07, 2008 9:50 am
Location: Dark Side Of The Moon

Re: Converting NSDate

Postby witness » Sat Nov 05, 2011 5:39 am

[myDate timeIntervalSinceReferenceDate] should give you an NSTimeInterval that's 100% identical to a CFAbsoluteTime. I think there are methods to convert between other date formats and CFAbsoluteTime. Alternately, I think you can use NSCalendarDate to get the elements corresponding to dateItems. All you'd have to do is combine them as a string with commas in between, I think.

Finally, NSDateFormatter can probably be used to generate other date formats. You can even use setFormat: to specify your own order, lengths, delimiters etc.
Cheers,
-- Uli Kusterer
"The Witnesses of TeachText are Everywhere..."
http://www.zathras.de
User avatar
witness
 
Posts: 57
Joined: Thu Jul 17, 2008 2:24 pm

Re: Converting NSDate

Postby Dave_Higgins » Sat Nov 05, 2011 7:36 am

witness wrote:Alternately, I think you can use NSCalendarDate to get the elements corresponding to dateItems. All you'd have to do is combine them as a string with commas in between, I think.


Some time ago I tried going that route, but IIRC, NSCalendarDate can provide all the dateItems, except for the last one... Day of the week. I searched around for ways to obtain that, and it got pretty messy.
My two favorite teams are Detroit and whoever's playing Chicago.
User avatar
Dave_Higgins
 
Posts: 454
Joined: Mon Jul 07, 2008 9:50 am
Location: Dark Side Of The Moon

Re: Converting NSDate

Postby HairyHighlandCow » Sat Nov 05, 2011 8:05 am

Code: Select all
NSString *dateDescription;
NSTimeZone *theLocalTimeZone;
   
theLocalTimeZone = [NSTimeZone localTimeZone];

NSCalendarDate *theDate = [theInputDate dateWithCalendarFormat:nil timeZone:theLocalTimeZone];   
                              
int year,month,day,hour,minute,second,theWeekday;

year = [theDate yearOfCommonEra];
month = [theDate monthOfYear];
day = [theDate dayOfMonth];
hour = [theDate hourOfDay];
minute = [theDate minuteOfHour];
second = [theDate secondOfMinute];
theWeekday = ([theDate dayOfWeek]) + 1;
            
dateDescription = [NSString stringWithFormat:@"%d,%d,%d,%d,%d,%d,%d,%@", year, month, day, hour, minute, second, theWeekday, [theDate descriptionWithCalendarFormat:@"%Z,%Y%m%d %H%M%S"]];
Externals, projects and software made with SC:
www.hairyhighlandcow.net/software/SC-projects.html
User avatar
HairyHighlandCow
 
Posts: 240
Joined: Sun Jul 06, 2008 1:45 pm
Location: London, UK

Re: Converting NSDate

Postby Dave_Higgins » Sat Nov 05, 2011 2:13 pm

Thanks, Alec... That'll do just fine.
My two favorite teams are Detroit and whoever's playing Chicago.
User avatar
Dave_Higgins
 
Posts: 454
Joined: Mon Jul 07, 2008 9:50 am
Location: Dark Side Of The Moon

Re: Converting NSDate

Postby witness » Sat Nov 05, 2011 4:51 pm

Dave_Higgins wrote:Some time ago I tried going that route, but IIRC, NSCalendarDate can provide all the dateItems, except for the last one... Day of the week. I searched around for ways to obtain that, and it got pretty messy.


Maybe you were thinking of something else, it has a -dayOfWeek message, that should suffice.
Cheers,
-- Uli Kusterer
"The Witnesses of TeachText are Everywhere..."
http://www.zathras.de
User avatar
witness
 
Posts: 57
Joined: Thu Jul 17, 2008 2:24 pm

Re: Converting NSDate

Postby Dave_Higgins » Sat Nov 05, 2011 8:36 pm

witness wrote:Maybe you were thinking of something else, it has a -dayOfWeek message, that should suffice.

Yeah... I don't know what it was... It was quite a while ago, and I have trouble remembering stuff like that if it's over a few days ago. :twisted: You know... Getting to that age where in order to learn something new I gotta kick out something that I already know... That kind of thing.

I have a feeling that I was just stuck in NSDate and trying to format it out with descriptionWithCalendarFormat, and didn't find a %code to get dayOfWeek. Might have been shy of it because of:
NSDate Class Reference wrote:Important: NSCalendarDate is slated for deprecation, and its use is strongly discouraged.
My two favorite teams are Detroit and whoever's playing Chicago.
User avatar
Dave_Higgins
 
Posts: 454
Joined: Mon Jul 07, 2008 9:50 am
Location: Dark Side Of The Moon


Return to Xcode and the Internals Toolbox

Who is online

Users browsing this forum: No registered users and 2 guests

cron