iOS Swift - 获取当前本地时间和日期时间戳

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

问题描述

我正在尝试制作一个考勤应用程序,但我对 iOS 和 Firebase 中的日期和时间感到非常困惑.

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

我使用日期作为 Key,这是我的 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
}

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

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?

推荐答案

为了将当前时间保存到 firebase 数据库,我使用了 Unic Epoch Conversation:

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

let timestamp = NSDate().timeIntervalSince1970

用于将 Unix 纪元时间解码为 Date().

and For Decoding Unix Epoch time to Date().

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

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)