5

I have an attribute in CoreData which accepts a date value. I just want to get the current date and save it in this format "dd/mm/yyyy" . But don't know how. Thanks

Luke97
  • 529
  • 3
  • 11
  • 27
  • 1
    CoreData can store `Date` objects natively. You shouldn't have to translate to a standard `String` format when going between your application and the database. For example, in my project I have some entities with properties of type `Date` which are declared in the `NSManagedObject` subclass as `@NSManaged var date: Date?`. They can be accessed directly without having to bother with any `String` conversions. – Michael Fourre Feb 01 '17 at 11:25

3 Answers3

6

If you're storing it as Date, then you have no control over the format until you try to present the returned value somewhere, and you just store Date()

If you're storing it as shown, then you need to use a DateFormatter to create the string you need

Russell
  • 5,436
  • 2
  • 19
  • 27
  • So when i set the value to save i just use "Date()" ? – Luke97 Feb 01 '17 at 11:23
  • Yes - https://developer.apple.com/reference/foundation/date/1780470-init The value will be stored in Date format - not 'dd/mm/yyyy', so if you want to display it like that, you need to format it before you use it later – Russell Feb 01 '17 at 11:25
  • So i would just format it when i get it back from core-data? – Luke97 Feb 01 '17 at 11:29
  • 2
    Yes - just like @Rock-Balbao has shown in his answer – Russell Feb 01 '17 at 11:30
3

Here you can store date in coreData as shown in format

let date = NSDate()
var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
var dateString = dateFormatter.string(from: date as Date)

store dateString in your coreData

Rocky Balboa
  • 784
  • 10
  • 25
  • indeed, but the point is you can **either** store a Date value or a string representation of it. You can't store a Date value with a format – Russell Feb 01 '17 at 11:28
  • @Russell I know. but the format he asked is in this format. So I gave him this answer. For another scenario he has your answer already – Rocky Balboa Feb 01 '17 at 11:29
0

To save dates in Core Data I have created the following extension

 extension Date {
     /**
      Formats a Date

      - parameters format: (String) for eg dd-MM-yyyy hh-mm-ss
      */
     func format(format:String = "dd-MM-yyyy hh-mm-ss") -> Date {
         let dateFormatter = DateFormatter()
         dateFormatter.dateFormat = format
         let dateString = dateFormatter.string(from: self)
         if let newDate = dateFormatter.date(from: dateString) {
             return newDate
         } else {
             return self
         }
     }
 }

To set a date value of a field

 let date = Date()
 entity?.setValue(date.format(), forKey: "updated_at")

Note: You may see date saved as timestamp when you open your database. Please refer to the following issue NSDate being saved as timestamp in CoreData

You can also pass you own date formats in the "format()" extension to get different types of dates in the application

Rajat Jain
  • 1,002
  • 1
  • 15
  • 24