[Swift5]日付の計算、日付の設定、日付の書式設定など

#1.はじめに
日付計算をVBのAddDaysのように足し引きしたいと思うことが多いのですが、毎回Calendar型に入れて書式設定をして表示、という流れが面倒くさい！

そこで、もうDate型を拡張して作ってしまおうというのがこれです。

よくわかんなくても、『4.使用例』のように.swiftファイルの一番下(Classの{}よりも外)とかにコピペすれば使えるんで試してもらえるといいかなと思います。

#2.コード
コードは以下になります。

```Swift:extension
extension Date{
    
    var startDayOfMonth: Date{
        let calendar = Calendar.current
        var components = DateComponents()
        components.year = calendar.component(.year, from: self)
        components.month = calendar.component(.month, from: self)
        components.day = 1
        components.hour = calendar.component(.hour, from: self)
        components.minute = calendar.component(.minute, from: self)
        components.second = calendar.component(.second, from: self)
        return calendar.date(from: components)!
    }
    
    var lastDayOfMonth: Date{
        let calendar = Calendar.current
        var components = DateComponents()
        components.year = calendar.component(.year, from: self)
        components.month = calendar.component(.month, from: self)
        components.day = calendar.range(of: .day, in: .month, for: self)?.count
        components.hour = calendar.component(.hour, from: self)
        components.minute = calendar.component(.minute, from: self)
        components.second = calendar.component(.second, from: self)
        return calendar.date(from: components)!
    }
    
    func addYear(add:Int) -> Date{
        let calendar = Calendar.current
        var components = DateComponents()
        components.year = calendar.component(.year, from: self) + add
        components.month = calendar.component(.month, from: self)
        components.day = calendar.component(.day, from: self)
        components.hour = calendar.component(.hour, from: self)
        components.minute = calendar.component(.minute, from: self)
        components.second = calendar.component(.second, from: self)
        return calendar.date(from: components)!
    }
    
    func addMonth(add:Int) -> Date{
        let calendar = Calendar.current
        var components = DateComponents()
        components.year = calendar.component(.year, from: self)
        components.month = calendar.component(.month, from: self) + add
        components.day = calendar.component(.day, from: self)
        components.hour = calendar.component(.hour, from: self)
        components.minute = calendar.component(.minute, from: self)
        components.second = calendar.component(.second, from: self)
        return calendar.date(from: components)!
    }
    
    func addDay(add:Int) -> Date{
        let calendar = Calendar.current
        var components = DateComponents()
        components.year = calendar.component(.year, from: self)
        components.month = calendar.component(.month, from: self)
        components.day = calendar.component(.day, from: self) + add
        components.hour = calendar.component(.hour, from: self)
        components.minute = calendar.component(.minute, from: self)
        components.second = calendar.component(.second, from: self)
        return calendar.date(from: components)!
    }
    
    func addHour(add:Int) -> Date{
        let calendar = Calendar.current
        var components = DateComponents()
        components.year = calendar.component(.year, from: self)
        components.month = calendar.component(.month, from: self)
        components.day = calendar.component(.day, from: self)
        components.hour = calendar.component(.hour, from: self) + add
        components.minute = calendar.component(.minute, from: self)
        components.second = calendar.component(.second, from: self)
        return calendar.date(from: components)!
    }
    
    func addMinute(add:Int) -> Date{
        let calendar = Calendar.current
        var components = DateComponents()
        components.year = calendar.component(.year, from: self)
        components.month = calendar.component(.month, from: self)
        components.day = calendar.component(.day, from: self)
        components.hour = calendar.component(.hour, from: self)
        components.minute = calendar.component(.minute, from: self) + add
        components.second = calendar.component(.second, from: self)
        return calendar.date(from: components)!
    }
    
    func addSecond(add:Int) -> Date{
        let calendar = Calendar.current
        var components = DateComponents()
        components.year = calendar.component(.year, from: self)
        components.month = calendar.component(.month, from: self)
        components.day = calendar.component(.day, from: self)
        components.hour = calendar.component(.hour, from: self)
        components.minute = calendar.component(.minute, from: self)
        components.second = calendar.component(.second, from: self) + add
        return calendar.date(from: components)!
    }
    
    func setYear(value:Int) -> Date{
        let calendar = Calendar.current
        var components = DateComponents()
        components.year = value
        components.month = calendar.component(.month, from: self)
        components.day = calendar.component(.day, from: self)
        components.hour = calendar.component(.hour, from: self)
        components.minute = calendar.component(.minute, from: self)
        components.second = calendar.component(.second, from: self)
        return calendar.date(from: components)!
    }
    
    func setMonth(value:Int) -> Date{
        let calendar = Calendar.current
        var components = DateComponents()
        components.year = calendar.component(.year, from: self)
        components.month = value
        components.day = calendar.component(.day, from: self)
        components.hour = calendar.component(.hour, from: self)
        components.minute = calendar.component(.minute, from: self)
        components.second = calendar.component(.second, from: self)
        return calendar.date(from: components)!
    }
    
    func setDay(value:Int) -> Date{
        let calendar = Calendar.current
        var components = DateComponents()
        components.year = calendar.component(.year, from: self)
        components.month = calendar.component(.month, from: self)
        components.day = value
        components.hour = calendar.component(.hour, from: self)
        components.minute = calendar.component(.minute, from: self)
        components.second = calendar.component(.second, from: self)
        
        return calendar.date(from: components)!
    }
    
    func setHour(value:Int) -> Date{
        let calendar = Calendar.current
        var components = DateComponents()
        components.year = calendar.component(.year, from: self)
        components.month = calendar.component(.month, from: self)
        components.day = calendar.component(.day, from: self)
        components.hour = value
        components.minute = calendar.component(.minute, from: self)
        components.second = calendar.component(.second, from: self)
        return calendar.date(from: components)!
    }

    func setMinute(value:Int) -> Date{
        let calendar = Calendar.current
        var components = DateComponents()
        components.year = calendar.component(.year, from: self)
        components.month = calendar.component(.month, from: self)
        components.day = calendar.component(.day, from: self)
        components.hour = calendar.component(.hour, from: self)
        components.minute = value
        components.second = calendar.component(.second, from: self)
        return calendar.date(from: components)!
    }
    
    func setSecond(value:Int) -> Date{
        let calendar = Calendar.current
        var components = DateComponents()
        components.year = calendar.component(.year, from: self)
        components.month = calendar.component(.month, from: self)
        components.day = calendar.component(.day, from: self)
        components.hour = calendar.component(.hour, from: self)
        components.minute = calendar.component(.minute, from: self)
        components.second = value
        return calendar.date(from: components)!
    }
    
    var getYear : Int{
        let calendar = Calendar.current
        return calendar.component(.year, from: self)
    }
    
    var getMonth : Int{
        let calendar = Calendar.current
        return calendar.component(.month, from: self)
    }
    
    var getDay : Int{
        let calendar = Calendar.current
        return calendar.component(.day, from: self)
    }
    
    var getHour : Int{
        let calendar = Calendar.current
        return calendar.component(.hour, from: self)
    }
    
    var getMinute : Int{
        let calendar = Calendar.current
        return calendar.component(.minute, from: self)
    }
    
    var getSecond : Int{
        let calendar = Calendar.current
        return calendar.component(.second, from: self)
    }
    
    
    func text(format:String,identifier:String = "ja_JP") -> String{
        let dateFormatter = DateFormatter()
        dateFormatter.locale = Locale(identifier: identifier)
        dateFormatter.dateFormat = format
        return dateFormatter.string(from: self)
    }
    
}

```

#3.説明
| 変数および関数     |型| 説明|
|:--|:-:|:--|
| startDayOfMonth  |Date| その月の初日を返す。|
| lastDayOfMonth   |Date|その月の最終日を返す。|
| add(Year,Month,Day,Hour,Minute,Second)|Date|それぞれ年や月などに引数を加えた日付を返す。|
| set(Year,Month,Day,Hour,Minute,Second)|Date|それぞれ年や月などに引数を設定した日付を返す。|
| get(Year,Month,Day,Hour,Minute,Second)|Int|それぞれ年や月などの値を返す|
| text(format:String,identifier:String = "ja_JP")|String|引数の書式の文字列を返す。identifierが未記入の場合は日本時間で返す。|

#4.使用例
実際に使うのはこんな感じです。
ViewDidLoad内のコメントがprint結果です。

```Swift:ViewController.swift
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        print(Date())
        //2019-06-06 04:30:34 +0000
        
        print(Date().text(format: "yyyy/MM/dd HH:mm:ss"))
        //2019/06/06 13:30:34
        
        print(Date().startDayOfMonth.text(format: "yyyy/MM/dd HH:mm:ss"))
        //2019/06/01 13:30:34
        
        print(Date().lastDayOfMonth.text(format: "yyyy/MM/dd HH:mm:ss"))
        //2019/06/30 13:30:34
        
        print(Date().setDay(value: 15).text(format: "yyyy/MM/dd HH:mm:ss"))
        //2019/06/15 13:30:34
        
        print(Date().addMinute(add: 10).text(format: "yyyy年MM月dd日(EEE) HH時mm分ss秒"))
        //2019年06月06日(木) 13時40分34秒
        
        print(Date().getHour)
        //13
        
    }


}

extension Date{
...    
}

```
#5.おわりに
こういうの誰かFrameWork作ってそうだなとは思うんですが探したり設定したりが面倒そうだったのでコピペでいけるよう自作しました。

みんなどうしてるんだろう。