sharon Hwk sharon Hwk - 1 year ago 111
iOS Question

current Date and Time - NSDate

I need to display the current Date and Time.

I have used ;

NSDate *currentDateNTime = [NSDate date];

I want to have the current date and time (Should display the system time and not GMT time).
The output should be in a
format and not

for example;

NSDate *currentDateNTime = [NSDate date];
// Do the processing....

NSDate *nowDateAndTime = .....; // Output should be a NSDate and not a NSString

Answer Source

Since all NSDate is GMT referred, you probably want this: (don'f forget that the nowDate won't be the actual current system date-time, but it's "shifted", so if you will generate NSString using NSDateFormatter, you will see a wrong date)

NSDate* currentDate = [NSDate date];
NSTimeZone* currentTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* nowTimeZone = [NSTimeZone systemTimeZone];

NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:currentDate];
NSInteger nowGMTOffset = [nowTimeZone secondsFromGMTForDate:currentDate];

NSTimeInterval interval = nowGMTOffset - currentGMTOffset;
NSDate* nowDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:currentDate];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download