1

How do I create a date object in swift 4 that isn't the current date? For example December 3, 2019 @ 2:35PM. Then, how Do I write that object to a document in a firestore database as a timestamp?

yambo
  • 1,388
  • 1
  • 15
  • 34
  • 1
    So you are just asking how to initialize a Date object with certain date components? – Leo Dabus Sep 12 '18 at 20:41
  • Or are you asking about how to write a timestamp typed field to a document, if you already have some other date type object to work with? – Doug Stevenson Sep 12 '18 at 20:49
  • Both aspects. I'm not sure how to initialize a date field in swift that is a specific date and time in the future, or how to write said object to a timestamp field in a specific document within the database. Sorry for the confusion, I've edited my question to try and make it clearer! – yambo Sep 12 '18 at 21:06

2 Answers2

1

You can create a DateFormatter object to convert native Date objects back and forth to strings and specify your dates as strings, but I don't recommend that.

Take a look at the Calendar class and the DateComponents class. The Calendar function with the signature

func date(from components: DateComponents) -> Date?

lets you use a DateComponents object to create a date.

So you might use code like this:

let calendar = Calendar.current
let components = DateComponents(
  calendar: calendar,
  year: 2019, 
  month: 12, 
  day: 3, 
  hour: 14, 
  minute: 39)

if let date = calendar.date(from: components) {
    print(DateFormatter.localizedString(
        from: date, 
        dateStyle: .medium, 
        timeStyle: .medium))
}

That would output

Dec 3, 2019 at 2:39:00 PM
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Thanks! Your solution works very well, but how would I send it to firestore? And why do you recommend against using a `DateFormatter`? Sorry for the misunderstanding, I'm very new to coding. – yambo Sep 13 '18 at 05:30
  • I haven't used FireStore before. A quick search revealed this: https://stackoverflow.com/questions/51008044/converting-firestore-timestamp-to-date-data-type – Duncan C Sep 13 '18 at 12:24
  • DateFormatters convert dates from/to strings, which is fragile. If you run the app on a device in a different locale, for example, the interpretation of the date format changes, and can break you date string handling. (You can force the locale, but it's and extra step) – Duncan C Sep 13 '18 at 12:25
  • When I receive the date back from the database I convert it to a `Date` object. Is it still unwise to use the `DateFormatter` to pull out the specific components in this case? Or is there a better way to read the specific components? – yambo Sep 13 '18 at 15:51
  • Better to use the Calendar class' `dateComponents(_:from:)` method, which will give you the requested components from your Date object. – Duncan C Sep 13 '18 at 18:05
0

if your dates are in string format like 2019/12/03 14:35

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy/MM/dd HH:mm"
let date = dateFormatter.date(from: "2019/12/03 14:35")

you can send date object to firestore like that

let data:[String: Any] = ["notCurrentDate":date]

let db = Firestore.firestore()

db.collection("data").document("document").setData(data) { err in
  if let err = err {
      print("Error writing document: \(err)")
  } else {
      print("Document successfully written!")
  }
}
snake_plissken
  • 109
  • 1
  • 2