Getting the seconds from a date


#1

Is there an easy way to get just the seconds element from an NSDate? I just want to grab the seconds from the current date and time to use as part of a randomizer, and I can’t seem to find a way to do it.


#2

An NSDate is simply the number of seconds since Jan 1, 2001 (or maybe its 2000? Check the docs, I can’t remember offhand).

When you say ‘get the seconds’, what do you mean? The seconds are in relation to something. If you wanted to, say, get the seconds between the NSDate and another NSDate, you can use timeIntervalSinceDate:, which returns seconds.


#3

[quote=“JoeConway”]An NSDate is simply the number of seconds since Jan 1, 2001 (or maybe its 2000? Check the docs, I can’t remember offhand).

When you say ‘get the seconds’, what do you mean? The seconds are in relation to something. If you wanted to, say, get the seconds between the NSDate and another NSDate, you can use timeIntervalSinceDate:, which returns seconds.[/quote]

What I was thinking of is trying to create a randomizer that gets the seconds in the current minute, as “32” or “2”, etc., from when the method calls for the seconds. If I used the “rand()” function, which is a typical non-random randomizer – you get the same consistent set of random numbers from it. I though that adding the seconds to the value provided would then make the number really random. Just a thought.

Brad


#4

Ah, yeah, using the current time as a seed for the random number. You’ll be better off just calling this every time the app starts:

srand(time(NULL));