How do I convert a UTC NSDate
to local timezone NSDate in Objective C or/and Swift?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Using if let syntax in switch statement
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- Enum with associated value conforming to CaseItera
Since no one seemed to be using
NSDateComponents
, I thought I would pitch one in... In this version, noNSDateFormatter
is used, hence no string parsing, andNSDate
is not used to represent time outside of GMT (UTC). The originalNSDate
is in the variablei_date
.i_anotherCalendar
could beNSCalendarIdentifierGregorian
or any other calendar. TheNSString
allowed fori_anotherTimeZone
can be acquired with[NSTimeZone knownTimeZoneNames]
, butanotherCalendar.timeZone
could be[NSTimeZone defaultTimeZone]
or[NSTimeZone localTimeZone]
or[NSTimeZone systemTimeZone]
altogether.It is actually
anotherComponents
holding the time in the new time zone. You'll noticeanotherDate
is equal toi_date
, because it holds time in GMT (UTC).Convert your UTC date to Local Date
Use Like This
I write this Method to convert date time to our LocalTimeZone
-Here (NSString *)TimeZone parameter is a server timezone
If you want local Date and time. Try this code:-
Table of formatting string parameters:
https://waracle.com/iphone-nsdateformatter-date-formatting-table/
If performance is a priority, you may want to consider using
strftime
https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man3/strftime.3.htmlHere input is a string currentUTCTime (in format 08/30/2012 11:11) converts input time in GMT to system set zone time