Time picker shows wrong time iphone sdk - ios

I have time picker in my app. I'm showing timePicker.date and the time is wrong.
NSLog is
NSLog (#"date : %#",[timePicker.date description]);
log is like date: 2012-02-07 17:00:01 +0000
i think that problem is in timezone
in ViewDidLoad i have this code
timePicker.locale = [NSLocale currentLocale];
timePicker.timeZone = [NSTimeZone localTimeZone];
but it isn't working...
Can somebody help me to solve my problem. Thanks

It is likely that the time picker is correctly applying time zone, but you're logging it using GMT rather than local time so that it just looks wrong. If you want to display the time returned from your time picker using local time, use NSDate's descriptionWithCalendarFormat:timeZone:locale: method, or use the NSDateFormatter class to get complete control over how your date is displayed.

Related

How to get currentUTC time in swift

I want to set count down timer in swift. I have an option is to get current time is Date() but this method is giving wrong date time when my device time set wrong.
Is it possible to get exact current UTC time in swift, so I will set count down timer with exact time remaining.
Thanks.
The Date class doesn't have a timezone, it's just a "point int the time line" that's the same for all the timezones. When you use a DateFormatter to convert a date to a string (or a string to a date) you can specify the timezone like this:
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
If you cannot trust the device date you will have to use a NTP service like this:
https://github.com/instacart/TrueTime.swift
https://github.com/jbenet/ios-ntp
Get Date and Time from Apple Server
Many times, I have faced the same issue if the user changed his current time then lot's of logic will disturb, Unfortunately, no luck because of Date() always returns your device date.
In well-known game Candy crush, We can not play it for a specific time if my points got over, But if I change device time to feature time then everything will be unlocked. This is the best example of your problem.
You can use below-given web service or your web service to achieve your requirements. Below are some
free API's which provides date and time.
Geonames
Timezonedb
TrueTime
In addition to other answers, you can write an extension for Date class to get formatted Data in specific TimeZone to make it as utility function for future use. Like
extension Date {
func dateInTimeZone(timeZoneIdentifier: String, dateFormat: String) -> String {
let dtf = DateFormatter()
dtf.timeZone = TimeZone(identifier: timeZoneIdentifier)
dtf.dateFormat = dateFormat
return dtf.string(from: self)
}
}
Now you can call it like
Date().dateInTimeZone(timeZoneIdentifier: "UTC", dateFormat: "yyyy-MM-dd HH:mm:ss");

How to get UTC universal time from CLLocation locations?

It looks like the timestamp property of CLLocation takes the device's current date and time... I have this code snippet:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *newLocation = [locations lastObject];
NSDate *eventDate = newLocation.timestamp;
long long locTimestamp = [date timeIntervalSince1970] * 1000;
NSLog(#"Loc timestamp: %lld:", locTimestamp);
}
At the moment I'm writing this, if I print the description of eventDate in the Xcode's debug area console, I get:
Printing description of eventDate:
2015-08-26 15:14:09 +0000
And if I change the date in the device's settings, I get that wrong date in evenDate:
Printing description of eventDate:
2015-07-25 15:16:33 +0000
Maybe I'm misunderstanding the NSDate that is returned in the timestamp property... it really takes the date and time you have set in the device, or it is just given the format of those device's settings?
The point is, I need to get UTC times of locations to report them to a server and make some comparisons between different devices, and if user changes the date and time settings of his device and my app sends wrong or "fake" timestamps, my app couldn't perform well...
It seems that it is possible in Android apps to get the NMEA data from GPS and then get the universal time of positions, but I can´t find that in iOS. Is it possible to get it in iOS?
I need help with this issue, thanks in advance
You can convert the timestamps into any calendar or timezone you want via the NSCalendar and NSDateFormatter and NSLocaleclasses. Docs here

NSDate as function parameter in Swift

I'm facing an issue if I pass NSDate as a function parameter.
My code is:
self.PassDate(responseDate) ; // Response date value = 2015-05-15T00:00:00
func PassDate(date:NSDate) {
// Here ideally date value should be same as responseDate value.. but date is coming as 2015-05-14T07:00:00 PDT
}
Why passing NSDate is changing the timezone/values?
Nothing is changing. Different ways of viewing dates present them using different time zones. The underlying date is not changed. If you view the date in the debugger it (usually) shows it in UTC.
Try logging the date outside your function and inside using println() calls. You should see the same value in both places if you view it the same way each time.

iOS NSDateFormatter behaving differently on device

I have the following code to create NSDate objects from NSString objects.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm aa M/dd/yyyy"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSString *day = #"3/26/2015";
NSString *time = #"10:24 PM";
NSString *dateString = [NSString stringWithFormat:#"%# %#", time, day];
NSDate *date = [dateFormatter dateFromString:dateString];
This piece of code works perfectly on the simulator, yielding the exact corresponding time in my time zone. However when I run this on a device with iOS 8, date is set to nil.
The format I'm using is supposed to work according to this page that is referenced by this Apple Developer page.
Any help or information on this would be much appreciated. Thanks in advance.
Edit: I'm trying to create NSDate objects from formatted NSString's, not get the date from the system in a predefined fromat. The possible duplicate of this question is probably closely related but I couldn't get the solution to work in my situation.
Edit 2: I've just noticed this problem only occurs when 24-Hour Time is enabled in Settings. However, there is no way for me to know which format the device owner is using, so I'm still in need of a solution.
When working with such strict date formats, you need to set the locale to avoid having issues with the device current locale when formatting dates. Otherwise, the NSDateFormatter will use the device's locale, which explains the fact that it happens only when you enable 24-Hour Time in Settings.
See Apple's documentation:
In all cases, you should consider that formatters default to using the user’s locale (currentLocale) superimposed with the user’s preference settings. If you want to use the user’s locale but without their individual settings, you can get the locale id from the current user locale (localeIdentifier) and make a new "standard” locale with that, then set the standard locale as the formatter's locale.
For example, in your case, you could use the en_US_POSIX:
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
dateFormatter.locale = enUSPOSIXLocale;

DatePicker not returning selected date iOS

I am trying to get the selected date from a DatePicker and all I get is the current system date. Any ideas why?
I am doing it through the simulator as I do not have a device. Here is the code I use to get the date.
[self scheduleLocalNotification:timerPicker.date];
I have also used this to see if it works:
NSDate *d = [timerPicker date];
[self scheduleLocalNotification:d];
After several reboots of XCode and no code changes it magically started working.

Resources