Meet Doshi
Meet Doshi

Reputation: 4259

How can I get the Correct Current Time in iOS?

I know this it the repeated question. I have reviewed many answers but still didn’t get any solution.

My question is, How can I get the Time of Device if user have set it manually..?

I have implemented one chatting app. But in this app, I have issues of timings. If user have set manually time then how can we identify that.?

I want to get correct current UTC time. So using this, I can identify the difference between device time and UTC time.

If we are using, NSDate *currentDateAndTime = [NSDate date];, then it returns only UTC time according to device, not the current UTC time.

Is there any easy way to find out this solution?

Any help would be appreciated.

Upvotes: 4

Views: 14684

Answers (6)

Sucharu Hasija
Sucharu Hasija

Reputation: 1126

This might solve your problem

NSDate * date = [NSDate date];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
        NSString *currentTime = [dateFormatter stringFromDate:date];
        NSLog(@"current time is:%@",currentTime);

Upvotes: 2

Anand Nimje
Anand Nimje

Reputation: 6251

You can use this code for getting current time:-

Swift 3

let date = Date()
let formatter = DateFormatter()
formatter.timeZone = TimeZone(abbreviation: "GMT+0:00") //Current time zone
formatter.dateFormat = "HH:MM a"
let currentTime: String = formatter.stringFromDate(date)
print("My Current Time is \(currentTime)")

Result **
Output : - "My Current Time is 11:05 AM\n"

Upvotes: 0

sgl0v
sgl0v

Reputation: 1417

There is no trusted time source in iOS. You just operate with monotonic and non-monotonic time.

Monotonic time (or absolute time) represents the absolute elapsed wall-clock time since some arbitrary, fixed point in the past. The CACurrentMediaTime() or mach_absolute_time return monotonic time.

Non-monotonic time represents the machine's best-guess as to the current wall-clock, time-of-day time. It can jump forwards and backwards as the system time-of-day clock is changed. The [NSDate date] call returns it.

As an option, you can take the trusted date/time from the http Date response header. Then save it and current monotonic time(CACurrentMediaTime()) in the keychain. Here is the way to get the current trusted time:

last known online time + (CACurrentMediaTime() - saved monotonic time)

Another solution would be to use NTP server.

Upvotes: 17

AkBombe
AkBombe

Reputation: 658

Try this:

swift:

let date = NSDate()
let dateFormatter = NSDateFormatter()
let timeZone = NSTimeZone(name: "UTC")

dateFormatter.timeZone = timeZone

println(dateFormatter.stringFromDate(date))

Obj-C:

NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];

[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeZone:timeZone];       
NSLog(@"%@", [dateFormatter stringFromDate:date]);

Upvotes: 0

user5938635
user5938635

Reputation:

use this code it helps you

NSDate * datee = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSString *localDateString = [dateFormatter stringFromDate:currentDate];

NSLog(@"%@",localDateString);

Upvotes: 1

Nicolas Buquet
Nicolas Buquet

Reputation: 3955

Use : NSDate *currentDateAndTime = [NSDate date];

This will get you the absolute date and time in GMT reference zone.

Upvotes: 0

Related Questions