0
0

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.

[Dart]DateTime型についてメモ

Posted at

はじめに

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

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

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

現在時刻を取得する

DateTime now = DateTime.now();
now;//2022-07-23 21:24:04.916

指定する

DateTime date = DateTime(2021,7,3,21,20,20,111);
date;//2021-07-03 21:20:20.111

年や月、日等を取得する

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メソッド(加算する)

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メソッド(減算する)

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メソッド(比較する)

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メソッド(未来か)

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メソッド(過去か)

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メソッド(同一か)

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メソッド(差分を取得)

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

曜日や月の取得も出来る

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 差分を取得

参考

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

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?