How to declare data type as Timestamp in ios - swift

Viewed 7364

I am a new iOS programming. I am creating a simple which integrate with Firestore. In Firestore i created collection and each documents contains many fields.

I have added Timestamp field in my document. And when i create model in xcode how can i declare my variable as Timestamp because i need to sort data on tableView base on Timestamp.

This is how Timestamp in Firestore looks like:

September 17, 2018 at 3:37:43 PM UTC+7

So, How can i write program and get current Timestamp like showing in Firestore

This is in programming:

struct RecentCallModel {

var call_answered: Bool?
var call_extension: String?
var call_type: String?
var details: String?
var duration:  String?
var title: String?
// How can i declare as Timestamp
var timestamp: ???

init(call_answered: Bool, call_extension: String, call_type: String, details: String , duration: String, timestamp: String,  title: String) {

    self.call_answered = call_answered
    self.call_extension = call_extension
    self.call_type = call_type
    self.details = details
    self.title = title
 }


}
3 Answers

Not acquainted with Firebase. But usually, in iOS, we store timestamps as TimeInterval which are type alias for Double.

class func getDateOnly(fromTimeStamp timestamp: TimeInterval) -> String { 
  let dayTimePeriodFormatter = DateFormatter()
  dayTimePeriodFormatter.timeZone = TimeZone.current 
  dayTimePeriodFormatter.dateFormat = "MMMM dd, yyyy - h:mm:ss a z" 
  return dayTimePeriodFormatter.string(from: Date(timeIntervalSince1970: timestamp)) 
} 

This is method how can we get current date and time in swift. and you also can declare timestamp as Double but i am not sure

// get the current date and time
let currentDateTime = Date()

// initialize the date formatter and set the style
let formatter = DateFormatter()
formatter.timeStyle = .medium
formatter.dateStyle = .long

// get the date time String from the date object
formatter.string(from: currentDateTime) 

Not sure what does your question explicitly imply, but you typically declare a timestamp value as a Double, something along the lines of var timestamp: Double. The retrieved data will look something like this 1537187800, and can be converted to an actual date and/or time using the following helper class

class DateAndTimeHelper {

static func convert(timestamp: Double, toDateFormat dateFormat: String) -> String {

        let date = Date(timeIntervalSince1970: timestamp)
        let dateFormatter = DateFormatter()
            dateFormatter.timeZone = NSTimeZone.local
            dateFormatter.locale = NSLocale.current
            dateFormatter.dateFormat = dateFormat

        return dateFormatter.string(from: date)

    }

}

Which can be used using the following syntax

var timestamp: Double = 0.0

// Perform some database querying action in order to retrieve the actual timestamp value from the server
DateAndTimeHelper.convert(timestamp: timestamp, toDateFormat: DATE_FORMAT)

Note that you should replace DATE_FORMAT by the format you'd want to follow. A good reference of the available formats can be found here.

Related