LoginSignup
2

More than 5 years have passed since last update.

指定日時のDateオブジェクを作る

Last updated at Posted at 2017-12-07

SwiftDateを利用しているのですが、意外と欲しいプロパティなどが見つからなかったので、Dateを拡張してみました。
SwiftDateも利用しているので、ご注意ください。

昨日の日付を取得

var yesterday: Int {
   return Date(timeIntervalSinceNow: -60*60*24).day
}

明日の日付を取得

var tomorrow: Int {
    return Date(timeIntervalSinceNow: 60*60*24).day
}

明後日の日付を取得

var dayAfterTomorrow:Int {
    let n:TimeInterval = (60*60*24) * 2
    return Date(timeIntervalSinceNow: n).day
}

00:00:00のyesterday Dateを取得

var dateYesterday: Date? {
    let calendar = Calendar.current
    return calendar.date(byAdding: .day, value: -1, to: calendar.startOfDay(for: base))
}

00:00:00のtomorrow Dateを取得

var dateTomorrow: Date? {
    let calendar = Calendar.current
    return calendar.date(byAdding: .day, value: 1, to: calendar.startOfDay(for: base))
}

00:00:00のday after tomorrow Dateを取得

var dateDayAfterTomorrow: Date? {
    let calendar = Calendar.current
    return calendar.date(byAdding: .day, value: 2, to: calendar.startOfDay(for: base))
}

00:00:00のToday Dateを取得

var dateToday: Date? {
    let date = Date()
    return date.ex.createDate(yyyy: date.year, MM: date.month, dd: date.day, hh: 0, mm: 0, ss: 0)
}

指定日時のDateオブジェクトを生成

func createDate(yyyy:Int, MM:Int, dd:Int, hh:Int, mm:Int, ss:Int) -> Date {
    let year:String     = yyyy.description
    let month:String    = String(format:  "%02d", MM)
    let day:String      = String(format:  "%02d", dd)
    let hour:String     = String(format:  "%02d", hh)
    let minute:String   = String(format:  "%02d", mm)
    let sec:String      = String(format:  "%02d", ss)
    let str:String      = "\(year)/\(month)/\(day) \(hour):\(minute):\(sec)"
    let dateFormater    = DateFormatter()
    dateFormater.locale = Locale(identifier: "ja_JP")
    dateFormater.dateFormat = "yyyy/MM/dd HH:mm:ss"
    let date = dateFormater.date(from: str)
    return date!
}

利用するときは、Targeted Extensionを利用されると思いますが、extension部分は以下の通りです。

extension Date: ExtensionCompatible {}

extension Extension where Base == Date {
    var yesterday: Int {
        return Date(timeIntervalSinceNow: -60*60*24).day
    }

    var tomorrow: Int {
        return Date(timeIntervalSinceNow: 60*60*24).day
    }

    var dayAfterTomorrow:Int {
        let n:TimeInterval = (60*60*24) * 2
        return Date(timeIntervalSinceNow: n).day
    }

    /// 00:00:00のyesterday Dateを取得
    var dateYesterday: Date? {
        let calendar = Calendar.current
        return calendar.date(byAdding: .day, value: -1, to: calendar.startOfDay(for: base))
    }

    /// 00:00:00のtomorrow Dateを取得
    var dateTomorrow: Date? {
        let calendar = Calendar.current
        return calendar.date(byAdding: .day, value: 1, to: calendar.startOfDay(for: base))
    }

    /// 00:00:00のday after tomorrow Dateを取得
    var dateDayAfterTomorrow: Date? {
        let calendar = Calendar.current
        return calendar.date(byAdding: .day, value: 2, to: calendar.startOfDay(for: base))
    }

    /// 00:00:00のToday Dateを取得
    var dateToday: Date? {
        let date = Date()
        return date.ex.createDate(yyyy: date.year, MM: date.month, dd: date.day, hh: 0, mm: 0, ss: 0)
    }

    /// 指定日時のDateオブジェクトを生成
    ///
    /// - Parameters:
    ///   - yyyy: yyyyyy
    ///   - MM: MM
    ///   - dd: dd
    ///   - hh: hh
    ///   - mm: mm
    ///   - ss: ss
    /// - Returns: Date
    func createDate(yyyy:Int, MM:Int, dd:Int, hh:Int, mm:Int, ss:Int) -> Date {
        let year:String     = yyyy.description
        let month:String    = String(format:  "%02d", MM)
        let day:String      = String(format:  "%02d", dd)
        let hour:String     = String(format:  "%02d", hh)
        let minute:String   = String(format:  "%02d", mm)
        let sec:String      = String(format:  "%02d", ss)
        let str:String      = "\(year)/\(month)/\(day) \(hour):\(minute):\(sec)"
        let dateFormater    = DateFormatter()
        dateFormater.locale = Locale(identifier: "ja_JP")
        dateFormater.dateFormat = "yyyy/MM/dd HH:mm:ss"
        let date = dateFormater.date(from: str)
        return date!
    }
}

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
2