Clash Royale CLAN TAG#URR8PPP
Get date in current time zone in objective - c?
NSArray *fromD = [setFreeHour valueForKey:@"fromDate"];
NSString *fromdate = fromD[0];
NSDateFormatter *fromDateFormatter = [[NSDateFormatter alloc]init];
[fromDateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate *fromDate = [fromDateFormatter dateFromString:fromdate];
Results :
In fromdate = 2018-07-24 11:41:25
In fromDate = 2018-07-24 06:11:25 +0000
So how can i get both the date (fromdate and fromDate) equal or fromDate in Local Time Zone
fromdate
String
fromDate
Date
String
1 Answer
1
NSDate
values represent a point in time irrespective of the local interpretation of that point, i.e. when it is midday where you are it is midnight somewhere else - same point in time, different local interpretation.
NSDate
So when you compare dates you convert them to the equivalent NSDate
representation first.
NSDate
To convert a date/time in text format to NSDate
format you use NSDateFormatter
. If there is no time zone specified in the text format you are using, or you do set the formatters time zone property, then your instance of NSDateFormatter
will use the current local time zone set on the machine it is running on. E.g. If your code the time difference between the text format and the resultant NSDate
value is 5 hours, so we can deduce the machine you used is in a local time
NSDate
NSDateFormatter
NSDateFormatter
NSDate
I need to compare these date. So how can i get both the date equal?
You do exactly what you did, convert your text format to an NSDate
value and then compare those values using the methods provided by NSDate
. That is you don't "get both equal", you convert each to its point in time and compare those.
NSDate
NSDate
HTH
BTW: It is inadvisable to use variable names, fromdate
and fromDate
, which differ in (minor) capitalisation, it makes the code hard to read, debug and maintain.
fromdate
fromDate
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You only have one date. Where is the 2nd date?
– rmaddy
16 hours ago