7

When I am printing the date

//getting the current date and time
self.date = [NSDate date];
NSLog(@"%@",date);

The date which I am getting is correct, but there is a delay in time by 6 hrs. My system time is correct.

Abdurrahman Mubeen Ali
  • 1,331
  • 1
  • 13
  • 19
Ramgopal
  • 81
  • 1
  • 1
  • 6
  • [NDate date] method returns you current date and time based on the device's locale. If you want to set to different locale, you have to set it manually with "descriptionWithLocale" which returns a string representation of the receiver using the given locale. – ldindu Jul 06 '13 at 11:44

5 Answers5

16

try this

NSLocale* currentLoc = [NSLocale currentLocale];
NSLog(@"%@",[[NSDate date] descriptionWithLocale:currentLoc]); 
Rok Jarc
  • 18,765
  • 9
  • 69
  • 124
Pratyusha Terli
  • 2,343
  • 19
  • 31
11

Make use of NSDateFormatter

NSDate *today = [NSDate date];

//Create the dateformatter object
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

//Set the required date format
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

//Get the string date
NSString *dateString = [dateFormatter stringFromDate:today];

//Display on the console
NSLog(dateString);
Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
  • 2
    Note that the dateFormatString is wrong. It will print the month as part of the time as well. And SS stands for something else as well. It should probably be `@"yyyy-MM-dd HH:mm:ss"` – luk2302 Jul 05 '15 at 17:52
  • @luk2302: thank you very much. I have updated my answer. – Shamsudheen TK Jan 08 '16 at 06:41
2

Logging an NSDate in the debugger is somewhat misleading as it gives you a calendar day and time for a particular time zone - UTC / GMT. However, NSDate has no inherent time zone or any inherent relationship to how humans perceive and think about dates at all. Instead, it is a timestamp. Classes like NSDateComponents, NSTimeZone, NSDateFormatter, and so on all exist to provide human context and formatting.

So what you see is the timestamp formatted to that particular format and UTC time zone, which is how NSDate will always appear when printed in the debugger or the console. If you were to calculate the time zone offset between UTC and your own time zone, you'd find that the date represents the time stamp you gave it, and not one however many hours off.

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
0

you can set current time zone for customizing your date format.

This link can help: https://stackoverflow.com/a/7213629/456471

Community
  • 1
  • 1
Danil
  • 1,780
  • 17
  • 24
0

The default date string representation is probably formatting the date as UTC, rather than your local time zone (the exact format that it will use is not defined, and may change from release to release, so you shouldn't rely on it). You should use the NSDateFormatter class if you need to format a date in a particular format (or with a particular time zone, including the local time zone); see the Data Formatting Guide and the NSDateFormatter Class Reference for more information.

JonathonW
  • 857
  • 7
  • 17