0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

実行した時刻を数字として取得する

Last updated at Posted at 2020-07-07

日時を数字として取得しドキュメントとして保存することで、Firebaseの非同期処理を乗り越えることができました。
他にもやり方はあると思いますが、これによってドキュメントを時系列でソートすることができます。
例)
2020年7月8日8時19分38分に実行
timeNumberDocument()
-->20070881938

import UIKit

func timeNumberDocument() -> Int {
    let dt = Date()
    var firstCha = ""
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "yyMMddHHmmss", options: 0, locale: Locale(identifier: "ja_JP"))
    var time = dateFormatter.string(from: dt)
    var timeNumber = time.compactMap { $0.hexDigitValue }.map({String($0)})
    for cha in timeNumber {
        var cha = cha
        firstCha += cha
    }
    return Int(firstCha) ?? 0
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?