I am building a CoreData UIKit application where I want to store the date and time same as the current timezone. Since NSDate doesn't have the ability to store timezone unlike Swift Date object, What's the best way to store the date with time zone? This is like a memory application where the record stored must have the same date as device when first created and if the user switches to a different timezone, The record must be fetched for the date irrespective of timezone.
What's the best way to store a Date with Timezone in Coredata?
You store the time zone's name (e.g. America/Los_Angeles) in a separate Core Data attribute like "timeZoneName" in your model.
Storing the time zone information:
From that stored name you can create the appropriate time zone object:
Storing the time zone information:
Code Block swift yourModel.timeZoneName = TimeZone.autoupdatingCurrent.name
From that stored name you can create the appropriate time zone object:
Code Block swift let timeZone = TimeZone(identifier: yourModel.timeZoneName)