iOS Swift - Get the Current Local Time and Date Timestamp

后端 未结 8 1686
鱼传尺愫
鱼传尺愫 2020-12-23 16:08

I\'m trying to make an attendance app and I am really confused about date and time in iOS and Firebase.

I use date as Key, this is the structure of my Firebase datab

相关标签:
8条回答
  • 2020-12-23 16:40

    First I would recommend you to store your timestamp as a NSNumber in your Firebase Database, instead of storing it as a String.

    Another thing worth mentioning here, is that if you want to manipulate dates with Swift, you'd better use Date instead of NSDate, except if you're interacting with some Obj-C code in your app.

    You can of course use both, but the Documentation states:

    Date bridges to the NSDate class. You can use these interchangeably in code that interacts with Objective-C APIs.

    Now to answer your question, I think the problem here is because of the timezone.

    For example if you print(Date()), as for now, you would get:

    2017-09-23 06:59:34 +0000
    

    This is the Greenwich Mean Time (GMT).

    So depending on where you are located (or where your users are located) you need to adjust the timezone before (or after, when you try to access the data for example) storing your Date:

        let now = Date()
    
        let formatter = DateFormatter()
    
        formatter.timeZone = TimeZone.current
    
        formatter.dateFormat = "yyyy-MM-dd HH:mm"
    
        let dateString = formatter.string(from: now)
    

    Then you have your properly formatted String, reflecting the current time at your location, and you're free to do whatever you want with it :) (convert it to a Date / NSNumber, or store it directly as a String in the database..)

    0 讨论(0)
  • 2020-12-23 16:48

    in Swift 5

    extension Date {
        static var currentTimeStamp: Int64{
            return Int64(Date().timeIntervalSince1970 * 1000)
        }
    }
    

    call like this:

    let timeStamp = Date.currentTimeStamp
    print(timeStamp)
    

    Thanks @lenooh

    0 讨论(0)
  • 2020-12-23 16:55

    If you just want the unix timestamp, create an extension:

    extension Date {
        func currentTimeMillis() -> Int64 {
            return Int64(self.timeIntervalSince1970 * 1000)
        }
    }
    

    Then you can use it just like in other programming languages:

    let timestamp = Date().currentTimeMillis()
    
    0 讨论(0)
  • 2020-12-23 16:56

    The simple way to create Current TimeStamp. like below,

    func generateCurrentTimeStamp () -> String {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy_MM_dd_hh_mm_ss"
        return (formatter.string(from: Date()) as NSString) as String
    }
    
    0 讨论(0)
  • 2020-12-23 17:00

    you can even create a function to return different time stamps depending on your necessity:

    func dataatual(_ tipo:Int) -> String {
            let date = Date()
            let formatter = DateFormatter()
            if tipo == 1{
                formatter.dateFormat = "dd/MM/yyyy"
            } else if tipo == 2{
                formatter.dateFormat = "yyyy-MM-dd HH:mm"
            } else {
                formatter.dateFormat = "dd-MM-yyyy"
            }
    
            return formatter.string(from: date)
        } 
    
    0 讨论(0)
  • 2020-12-23 17:04

    If you code for iOS 13.0 or later and want a timestamp, then you can use:

    let currentDate = NSDate.now
    
    0 讨论(0)
提交回复
热议问题