Need print information [NSDate date]

When i print the date

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

The date I receive is correct, but there is a delay time of 6 hours. My system time is correct.

+7
source share
5 answers

Use 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); 
+10
source

try it

 NSLocale* currentLoc = [NSLocale currentLocale]; NSLog(@"%@",[[NSDate date] descriptionWithLocale:currentLoc]); 
+16
source

The NSDate in the debugger is somewhat misleading, as it gives you the calendar day and time for a specific time zone - UTC / GMT. However, NSDate does not have an inherent time zone or any inherent relation to how people generally perceive and think about dates. Instead, it is a timestamp. Classes like NSDateComponents , NSTimeZone , NSDateFormatter , etc. All exist to provide human context and formatting.

So you see the timestamp formatted in this particular format and the UTC time zone, namely, how NSDate will always be displayed when printing in the debugger or console. If you were to calculate the time zone offset between UTC and your time zone, you will find that the date is a timestamp that you gave it, and not once every few hours.

+2
source

You can set the current time zone to adjust the date format.

This link may help: stack overflow

0
source

The default date string representation probably formats the date as UTC and not your local time zone (the exact format it will use is undefined and can change from release to release, so you should not rely on it). You should use the NSDateFormatter class if you need to format the date in a specific format (or with a specific time zone, including local time zone); see the Data Formatting Guide and the NSDateFormatter Class Reference for more information. information.

0
source

All Articles