0

I know this question has already been answered many times like:

  1. here
  2. here
  3. here
  4. and more

but this shit isn't working and I have no idea why. This is what I already did:

//First try
int currentTime  = [[NSDate date] timeIntervalSince1970] *-1000;
//Second try
int currentTime  = [[NSDate date] timeIntervalSince1970];
NSInteger milliseconds      = currentTime*1000;
//Third try
float currentTime  = [[NSDate date] timeIntervalSince1970];
currentTime  = currentTime * 1000;
//Fourth try
long currentTime  = [[NSDate date] timeIntervalSince1970];
currentTime  = currentTime * 1000;

But this always prints something totally different out like 260694392. When I just do

float currentTime  = [[NSDate date] timeIntervalSince1970];

It gives me the correct time in seconds, but now I also want to multiply it by 1000 to get it as milliseconds

Cœur
  • 34,719
  • 24
  • 185
  • 251
Musterknabe
  • 5,252
  • 12
  • 56
  • 113
  • Why don't you use `NSTimeInterval` for getting and storing the required value? – Akshat Singhal Dec 20 '13 at 10:30
  • Milliseconds since 1970 is too large to fit in a 32-bit integer, it needs about 39 bits. – Barmar Dec 20 '13 at 10:32
  • So what would be the best way to store miliseconds then? And how would I do it? As you can see I also tried it with int, float, nsinteger and long. @AkshatSinghal how would I use NSTimeInterval in combination with the miliseconds? – Musterknabe Dec 20 '13 at 10:37
  • uint64 should be long enough. – Larme Dec 20 '13 at 10:45
  • Or just use `NSNumber`. It will take care of everything. `NSNumber *msec = @(currentTime*1000);` – Ivan Genchev Dec 20 '13 at 10:53
  • I tried it with NSNumber but this still prints out something like this `262555392` What is the problem?? – Musterknabe Dec 20 '13 at 10:56
  • uint64_t worked! Thanks Ivan! – Musterknabe Dec 20 '13 at 10:59
  • You should also use `NSTimeInterval`, as the others suggested. After all `timeInterval...` is returning `NSTimeInterval`, which is `typedef`ed as `double` and actually `double` is big enough to hold that value as well. See this: https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaTouch64BitGuide/Major64-BitChanges/Major64-BitChanges.html. It's 8 bytes on both 32 and 64 bit. – Ivan Genchev Dec 20 '13 at 11:09

0 Answers0