Get date in current time zone in objective - c?

The name of the pictureThe name of the pictureThe name of the pictureClash 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





You only have one date. Where is the 2nd date?
– rmaddy
16 hours ago





one is fromdate and other is fromDate @rmaddy
– Mukie
10 hours ago





fromdate is a String. fromDate is a Date created from the String. So there is only one date. There is nothing to compare.
– rmaddy
7 hours ago


fromdate


String


fromDate


Date


String





I would highly recommend using variable names that are more obvious. Having two entirely different variables that differ only by a capital letter half way through is being intentionally obtuse.
– Fogmeister
2 mins ago




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





can i get the formatted code
– Mukie
10 hours ago





@Mukie - I'm sorry, but I've no idea what formatted code you are asking for. If you are asking for code I have written there is none, I've simply tried to explain how date/ties work. Is there something in the answer that is unclear to you? If there is make it clear what and I'll see if I can make it clearer.
– CRD
4 mins ago






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.

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

How to scale/resize CVPixelBufferRef in objective C, iOS