-1

I am using a MacBook developing my iOS app & run it on a real iPhone. Both my MacBook & my iPhone show me current time is 2016-08-11 13:18

But at the same time, the following code shows me the current time that is 3 hours earlier than now:

NSLog(@"NOW = %@",[NSDate date]); // it prints out 2016-08-11 10:18:17 +0000

But NSDate documentation tells me the +date function returns current date and time. Why I get a date time three hours earlier then?

Leem.fin
  • 40,781
  • 83
  • 202
  • 354
  • When you print the date it shows it in UTC (+0000). I guess you are in a Timezone 3 hours later than UTC – Paulw11 Aug 11 '16 at 10:34

1 Answers1

0

When you log an NSDate using NSLog it is always shown in UTC. It sounds like you are in the UTC+3 time zone, so a time shown in UTC will be 3 hours earlier.

If you want to display a date in your local time zone create a date formatter and use it to display the date instead. (By default date for matters use the user's local time zone.)

An NSDate does not have an inherent time zone. An NSDate records an instant in time. You have to use a time zone in order to look at it.

Your date is correct. You are just displaying it in a way that's confusing.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • How can I create a date which is the same as current system time in my iphone? – Leem.fin Aug 11 '16 at 10:35
  • See the duplicate question the others linked to. – Duncan C Aug 11 '16 at 10:36
  • it doesn't say how to create NSDate for current time, it only says use dateformatter which is not I want – Leem.fin Aug 11 '16 at 10:41
  • It isn't the creation, it is the display. You need to use a NSDateFormatter to display the date in the local Timezone. 13:18 in your Timezone is 10:18 in UTC, so the date itself is correct – Paulw11 Aug 11 '16 at 10:42
  • But I need to use a NSDate object which represents the current time. I simply need that NSDate object. That's it. Not displaying a date string. – Leem.fin Aug 11 '16 at 10:43
  • See the edit to my answer. You already HAVE a date that's correct. You are just getting confused by the way NSLog displays your date in UTC instead of your local time zone. – Duncan C Aug 11 '16 at 10:46
  • 1
    That is what you have. – Paulw11 Aug 11 '16 at 10:46
  • @Leem.fin `[NSDate date]` **is** the current time, but `NSLog` prints it always in UTC. If you want to consider the time zone use `NSDateFormatter` as suggested in the answer and in the related link. If you don't need to print it, just use it as it is. – vadian Aug 11 '16 at 10:48
  • No.No, you all misunderstood what I meant. I know NSLog print out the object supposed to display, I know I should use formatter to set system timezone so that it printout correctly.But using nslog is just my demo purpose to show the problem. What I am trying to say is, in my project, I need a NSDate object which represents the current system date time.I need to use that object. Not just log things out. So, how to get a NSDate object (please forget NSLog) represents current system time? – Leem.fin Aug 11 '16 at 10:51
  • I think I found the answer here : http://stackoverflow.com/questions/7485493/get-nsdate-from-nsdate-adjusted-with-timezone , NSCalendar is the answer I need. But thank you anyhow. – Leem.fin Aug 11 '16 at 10:53
  • 1
    Once again, `[NSDate date]` is the current date and time. Period. – vadian Aug 11 '16 at 10:54
  • @vadian, once again, I am asking for the current SYSTEM date and time, not UTC. – Leem.fin Aug 11 '16 at 10:57
  • Please create your SYSTEM date and time with NSCalendar and compare it with `[NSDate date`]... – vadian Aug 11 '16 at 10:59
  • Yes, I created and tested, with NSCalendar, I can set timezone to [NSTimeZone systemTimeZone], then, get the NSDate, which is different than [NSDate date]. Because [NSDate date] is UTC time (3 hours earlier than my SYSTEM time in my case), but the NSCalendar way is returning me the SYSTEM time. Hope this helps explain what I am asking. – Leem.fin Aug 11 '16 at 11:01
  • NSDate * dateGMT = [NSDate date]; NSTimeInterval secondsFromGMT = [[NSTimeZone localTimeZone] secondsFromGMT]; [dateGMT dateByAddingTimeInterval:secondsFromGMT]; – Florian Burel Aug 11 '16 at 12:12
  • Leem, your assumptions are wrong. An NSDate is a date is a date. If you request the current date using `[NSDate date]` from your time zone and I do the same thing at the same instant but from a different time zone, the dates we get back will be identical. An NSDate does not have a time zone. Where time zones come in is when you display the date. – Duncan C Aug 11 '16 at 16:35
  • 1
    @FlorianBurel, that is bad advice. Don't convert one date to another to deal with time zones. Use a calendar or a date formatter to VIEW a date in a specific time zone. – Duncan C Aug 11 '16 at 16:36