0
2

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 1 year has passed since last update.

SwiftDateでの日本基準の現在時刻取得

Posted at

少しつまずいたので、自分の備忘録としてメモ

DateInRegionを使って、日本をregionとした現在時刻を取得しても、US基準の現在時刻の取得になってしまう

sampleBefore.swift
import SwiftDate

let date = Date()
let japan = Region(calendar: Calendars.gregorian, zone: Zones.asiaTokyo, locale: Locales.japanese)

DateInRegion(year: date.year, month: date.month, day: date.day, hour: date.hour, minute: date.minute, second: date.second, region: japan)

SwiftDateのstructのプロパティであるdefaultRegionに日本を指定すれば、日本基準の現在時刻を取得できる

sampleAfter.swift
import SwiftDate
let date = Date()
let japan = Region(calendar: Calendars.gregorian, zone: Zones.asiaTokyo, locale: Locales.japanese)
SwiftDate.defaultRegion = japan

Date(year: date.year, month: date.month, day: date.day, hour: date.hour, minute: date.minute, second: date.second)

参考URL

SwiftDate - Github
SwiftDate の使用方法について (1) - Qiita

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?