How can I get the Correct Current Time in iOS?

江枫思渺然 提交于 2020-12-04 05:54:58

问题


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.


回答1:


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.




回答2:


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);



回答3:


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);



回答4:


Use : NSDate *currentDateAndTime = [NSDate date];

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




回答5:


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]);



回答6:


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"


来源:https://stackoverflow.com/questions/37526014/how-can-i-get-the-correct-current-time-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!