LoginSignup
3
0

More than 3 years have passed since last update.

【Swift】現在時刻を数値で取得

Posted at

どういうことか

例えば 12時34分 という時刻から 1234という数値を取得したい。
Swiftの日時取得は初めてで DateFormatter を使うというのはたくさん見つけたんだけど、文字列型で取得されて、それをうまくキャストすることが(ぼくには)できなかった。
なんとか見つけた方法でうまくいった。

Calendar

let now = Date()

let time = Calendar.current.dateComponents([.hour, .minute], from: now)

print(time.hour!) 
print(time.minute!)

PHPで型に甘えてたのかなぁとぼんやり思ってます。
それにしてもこんな単純なことでこんなに手間かかるもんなんでしょうか。簡単な方法あったらぜひ教えていただきたいです。

3
0
2

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
3
0