[Dart]DateTime型についてメモ

# はじめに
未経験からエンジニアに転職して4ヶ月ほど経ちました。
毎日壁にぶつかりながらも楽しく働いています。

普段はrailsを扱っていますが、最近Flutterも触ってみたいなーと思って休日はDartを勉強しております。

今回はDateTime型について学んだのでアウトプットしていきます。

## 現在時刻を取得する
```dart
DateTime now = DateTime.now();
now;//2022-07-23 21:24:04.916
```
## 指定する
```dart
DateTime date = DateTime(2021,7,3,21,20,20,111);
date;//2021-07-03 21:20:20.111
```
## 年や月、日等を取得する
```dart
DateTime now = DateTime.now();
now;//2022-07-23 22:21:21.950
now.year;//2022
now.month;//7
now.day;//23
now.hour;//22
now.minute;//21
now.second;//21
now.millisecond;//950
```

## addメソッド(加算する)
```dart
DateTime now = DateTime.now();
now;//2022-07-23 22:58:05.875
now.add(Duration(days: 1));//2022-07-24 22:58:05.875
now.add(Duration(days: -1));//2022-07-22 22:58:05.875
```

## subtractメソッド(減算する)
```dart
DateTime now = DateTime.now();
now;//2022-07-23 22:58:05.875  
now.subtract(Duration(days: 1));//2022-07-22 23:31:13.461
now.subtract(Duration(days: -1));//2022-07-24 23:31:13.461
```
## compareToメソッド(比較する)
```dart
DateTime now = DateTime.now();
DateTime yesterday = now.subtract(Duration(days: 1));
DateTime tomorrow = now.add(Duration(days: 1));
  
now.compareTo(now);//0
now.compareTo(yesterday);//1
now.compareTo(tomorrow);//-1
```

## isBeforeメソッド（未来か）
```dart
DateTime now = DateTime.now();
DateTime yesterday = now.subtract(Duration(days: 1));
DateTime tomorrow = now.add(Duration(days: 1));

print(now.isBefore(now));//false
print(now.isBefore(tomorrow));//true
print(now.isBefore(yesterday));//false
```

## isAfterメソッド（過去か）
```dart
DateTime now = DateTime.now();
DateTime yesterday = now.subtract(Duration(days: 1));
DateTime tomorrow = now.add(Duration(days: 1));
now.isAfter(now);//false
now.isAfter(tomorrow);//false
now.isAfter(yesterday);//true
```

## isAtSameMomentAsメソッド（同一か）
```dart
DateTime now = DateTime.now();
DateTime yesterday = now.subtract(Duration(days: 1));
DateTime tomorrow = now.add(Duration(days: 1));

now.isAtSameMomentAs(now);
now.isAtSameMomentAs(tomorrow);
now.isAtSameMomentAs(yesterday);
```

## differenceメソッド（差分を取得）
```dart
DateTime now = DateTime.now();
DateTime yesterday = now.subtract(Duration(days: 1));
DateTime tomorrow = now.add(Duration(days: 1));

now.difference(yesterday).inDays;//1
now.difference(yesterday).inHours;//24
now.difference(yesterday).inMinutes;//1,440
now.difference(yesterday).inSeconds;//86,400

now.difference(tomorrow).inDays;//-1
now.difference(tomorrow).inHours;//-24
now.difference(tomorrow).inMinutes;//-1,440
now.difference(tomorrow).inSeconds;//-86,400
```

## 曜日や月の取得も出来る
```dart
DateTime.january;//1
DateTime.february;//2
DateTime.march;//3
DateTime.april;//4
DateTime.may;//5
DateTime.june;//6
DateTime.july;//7
DateTime.august;//8
DateTime.september;//9
DateTime.october;//10
DateTime.november;//11
DateTime.december;//12

DateTime.monday;//1
DateTime.tuesday;//2
DateTime.wednesday;//3
DateTime.thursday;//4
DateTime.friday;//5
DateTime.saturday;//6
DateTime.sunday;//7
```

## まとめ
|  メソッド  |  何が出来る？する？  |
| ---- | ---- |
|  .now | 現在時刻を取得   |
|  .year, .month, etc...  |  年や月を取得  |
|  .add  |  加算する  |
|  .subtract  |  減算する  |
|  .compareTo  |  比較  |
|  .isBefore |  未来か  |
|  .isAfter |  過去か  |
|  .isAtSameMomentAs |  同一か  |
|  .difference |  差分を取得  |

### 参考
https://api.dart.dev/stable/2.10.5/dart-core/DateTime-class.html

読んで頂きありがとうございました!