cocoa - Get current NSDate in timestamp format -


i have basic method gets current time , sets in string. however, how can format current date & time in unix since-1970 timestamp format?

here code:

nsdate *currenttime = [nsdate date]; nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; [dateformatter setdateformat:@"hh-mm"]; nsstring *resultstring = [dateformatter stringfromdate: currenttime]; 

is possible use nsdateformatter change 'resultstring' timestamp?

here's use:

nsstring * timestamp = [nsstring stringwithformat:@"%f",[[nsdate date] timeintervalsince1970] * 1000]; 

(times 1000 milliseconds, otherwise, take out)

if you're using time, might nice declare macro

#define timestamp [nsstring stringwithformat:@"%f",[[nsdate date] timeintervalsince1970] * 1000] 

then call this:

nsstring * timestamp = timestamp; 

or method:

- (nsstring *) timestamp {     return [nsstring stringwithformat:@"%f",[[nsdate date] timeintervalsince1970] * 1000]; } 

as timeinterval

- (nstimeinterval) timestamp {     return [[nsdate date] timeintervalsince1970] * 1000; } 

note:

the 1000 convert timestamp milliseconds. can remove if prefer timeinterval in seconds.

swift

if you'd global variable in swift, use this:

var timestamp: string {     return "\(nsdate().timeintervalsince1970 * 1000)" } 

then, can call it

println("timestamp: \(timestamp)") 

again, *1000 miliseconds, if you'd prefer, can remove that. if want keep nstimeinterval

var timestamp: nstimeinterval {     return nsdate().timeintervalsince1970 * 1000 } 

declare these outside of context of class , they'll accessible anywhere.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -