7

I'm an objective-c beginner and I was assigned to create an iPhone app for our client. I have some background with Java but almost no experience in this objective-c and this is my first time to developping a complete application...

Anyway, I'm currently stack at several problems. One of those problem is that I need to send an integer value for PHP's date function from my iOS app. I've been searching around for the solution, but all of them are dealing with opposite ways (int to NSDate), not NSDate to integer value.

I tried solutions like answered here but it's obvious it returns double, not an integer...
Or this but this couldn't get the System time.

I know I could get current system's NSDate with:

NSDate *theDay = [NSDate dateWithTimeIntervalSinceNow:[[NSTimeZone systemTimeZone] secondsFromGMT]];  

But I could not figure out how to convert this to an integer (or long) value.

I just need to get the same value as we can get in Java with System.currentTimeMillis().

Also, this is my first time to ask questions here in stackoverflow. So please let me know if there's anything I should do/not to do when posting questions here, etc.

Thank you.

Community
  • 1
  • 1
Faust V
  • 667
  • 1
  • 9
  • 19

1 Answers1

7

To get the current date, you should use this:

NSDate *theDate = [NSDate date];

To get it in seconds since January 1st, 1970, as a double, you would use:

NSTimeInterval seconds = [[NSDate date] timeIntervalSince1970];

To get it in ms, simply multiply the previous value by 1000 and let the compiler cast it into an integer without needing any additional code:

long long milliseconds = [[NSDate date] timeIntervalSince1970] * 1000;

And as @HotLicks points out, while System.currentTimeMillis() is in GMT, if you need the local time, you could use:

long long milliseconds = ([[NSDate date] timeIntervalSince1970] 
                        + [[NSTimeZone defaultTimeZone] secondsFromGMT]) * 1000; 

(I think that most web services will want GMT though.)

lnafziger
  • 25,760
  • 8
  • 60
  • 101
  • 1
    It should be noted that NSDate is, by default/design, relative to UTC/GMT, and includes no timezone info. If one wants "local" time the timezone offset (appropriately scaled to seconds or milliseconds) must be added. – Hot Licks Nov 11 '12 at 04:09
  • 2
    @HotLicks: It should also be noted that `System.currentTimeMillis()` (in java) returns the time in UTC. ;-) – lnafziger Nov 11 '12 at 04:27
  • @lnafziger Thank you for quick and clear explanation and solutions! I actually tried this before but it didn't return the right result from our PHP server. However, I run some quick test in Java now and I can confirm that your code returns the same result as Java's currentTimeMillis. As Hot Licks pointed out, probably I need to add timezone offset on my own (so, for me there's actually no "easy way" to get the value with timezone included "automatically", with simple single line of method). Thank you both for taking time to read my question, and great help! – Faust V Nov 11 '12 at 05:01
  • @lnafziger -- Yeah, I couldn't remember whether currentTimeMillis was UTC or not (though I should have, since I implemented it once), but also I figured it wouldn't hurt to remind Faust of the timezone issue. – Hot Licks Nov 11 '12 at 13:41
  • 1
    @FaustV -- Actually, you probably can get it all with one single line -- something like `[[NSDate date] timeIntervalSince1970] + [[NSTimeZone defaultTimezone] offsetInSecondsFromGMT]` -- though I may have something slightly wrong there. – Hot Licks Nov 11 '12 at 15:58
  • Yup, you beat me to it... I was going to post this when I got back to my computer. :) I'll add it to my answer so that it is clear for people in the future though. – lnafziger Nov 11 '12 at 19:09