iOS Swift - 获取当前本地时间和日期时间戳 [英] iOS Swift - Get the Current Local Time and Date Timestamp

查看:1224
本文介绍了iOS Swift - 获取当前本地时间和日期时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用date作为Key,这是一个出席应用程序,我对iOS和Firebase中的日期和时间感到困惑。

   -  Employees 
--Unique_ID
- 详细
姓名:约翰
- 出勤
- 日期今天
时间:8:00 AM
超时:5:00 PM
BreakStart:12:00 PM
BreakFinish:1:00 PM

这是我的代码来获取我用作Key的日期时间戳

 重写func viewDidLoad(){
super.viewDidLoad()

let now = NSDate()
let nowTimeStamp = self.getCurrentTimeStampWOMiliseconds(dateToConvert:now)

//我将此日期保存为Firebase
dateToday = nowTimeStamp
}


func getCurrentTimeStampWOMiliseconds(dateToConvert:NSDate) - >字符串{
let objDateformat:DateFormatter = DateFormatter()
objDateformat.dateFormat =yyyy-MM-dd
let strTime:String = objDateformat.string(from:dateToConvert as Date)
let objUTCDate:NSDate = objDateformat.date(from:strTime)! as NSDate
let milliseconds:Int64 = Int64(objUTCDate.timeIntervalSince1970)
let strTimeStamp:String =\(milliseconds)
return strTimeStamp
}

但是当我把它转换回日期我得到2017-09-22 16:00:00 +0000,这是错误的,因为它是9月23日在我的位置。



什么是正确的代码使用,以便我可以得到正确的日期时间戳和时间戳记?

解决方案

为了节省当前时间到firebase数据库我使用Unic Epoch对话:

  let timestamp = NSDate()。timeIntervalSince1970 

和解密Unix Epoch time to Date()。

  let myTimeInterval = TimeInterval(timestamp)
let time = NSDate timeIntervalSince1970:TimeInterval(myTimeInterval))


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 database.

--Employees
  --Unique_ID
     --Details
          Name: John
     --Attendance
          --dateToday
              Timein: 8:00 AM
              Timeout: 5:00 PM
              BreakStart: 12:00 PM
              BreakFinish: 1:00 PM

This is my code to get the date timestamp I used as Key

 override func viewDidLoad() {
     super.viewDidLoad()

     let now = NSDate()
     let nowTimeStamp = self.getCurrentTimeStampWOMiliseconds(dateToConvert: now)

     // I save this dateToday as Key in Firebase
     dateToday = nowTimeStamp
}


func getCurrentTimeStampWOMiliseconds(dateToConvert: NSDate) -> String {
    let objDateformat: DateFormatter = DateFormatter()
    objDateformat.dateFormat = "yyyy-MM-dd"
    let strTime: String = objDateformat.string(from: dateToConvert as Date)
    let objUTCDate: NSDate = objDateformat.date(from: strTime)! as NSDate
    let milliseconds: Int64 = Int64(objUTCDate.timeIntervalSince1970)
    let strTimeStamp: String = "\(milliseconds)"
    return strTimeStamp
}

But when I convert it back to date I get 2017-09-22 16:00:00 +0000, which is wrong because it is 23rd of September in my location.

What is the right code to use so that I can get the correct date timestamp and time timestamp?

解决方案

For saving Current time to firebase database I use Unic Epoch Conversation:

let timestamp = NSDate().timeIntervalSince1970

and For Decoding Unix Epoch time to Date().

let myTimeInterval = TimeInterval(timestamp)
let time = NSDate(timeIntervalSince1970: TimeInterval(myTimeInterval))

这篇关于iOS Swift - 获取当前本地时间和日期时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆