Flutter/Dart

[Dart] DateUtils Class 정리 (날짜 작업을 위한)

주톨 2024. 1. 9. 13:56
728x90

 

 

 

flutter dart의 날짜 처리 작업을 위해 제공하는 DateUtils Class를 알아보겠습니다.

https://api.flutter.dev/flutter/material/DateUtils-class.html

 

DateUtils class - material library - Dart API

Utility functions for working with dates. Constructors DateUtils() Properties hashCode → int The hash code for this object. read-onlyinherited runtimeType → Type A representation of the runtime type of the object. read-onlyinherited Methods noSuchMetho

api.flutter.dev

 

 

 

import 'package:flutter/material.dart';

 

해당 패키지에서 기본 제공 됩니다.

 

DateUtils의 Methods는 전부 Static으로 되어있기 때문에 따로 객체를 만들지 않고 사용이 가능합니다.

순서대로 알아보겠습니다.

 

 

 

 

1. addDaysToDate

 

addDaysToDate(DateTime date, int days) => DateTime

 

인자로 받은 date에 days만큼의 일수를 더한 DateTime(자정)을 반환합니다.

 

*주의해야 할 점은 (자정)을 반환하기 때문에 시간이 설정되어 있어도 자정으로 변환해서 반환됩니다.

void main() {
  final nowDate = DateTime(2024,1,9);
  final DateTime a = DateUtils.addDaysToDate(nowDate, 3);
  print(a);
  ///출력 2024-01-12 00:00:00.000
}

 

 

* 음수를 넣으면 이전 일을 구합니다.

void main() {
  final nowDate = DateTime(2024,1,9);
  final DateTime a = DateUtils.addDaysToDate(nowDate, -3);
  print(a);
  ///출력 2024-01-06 00:00:00.000
}

 

 

 

2. addMonthsToMonthDate

addMonthsToMonthDate(DateTime monthDate, int monthsToAdd) => DateTime

 

인자로 받은 monthDate에 monthsToAdd 만큼의 월 수를 더하고 일수는 (1일)로 바꾼 DateTime(자정)을 반환합니다.

 

* 일은 1일로 바뀌고, 시간은 자정으로 반환됩니다.

void main() {
  final nowDate = DateTime(2024,1,9,14,10,50);
  final DateTime a = DateUtils.addMonthsToMonthDate(nowDate, 3);
  print(a);
  ///출력 2024-04-01 00:00:00.000
}

 

 

* 음수를 넣으면 이전 월을 구합니다.

void main() {
  final nowDate = DateTime(2024,1,9,14,10,50);
  final DateTime a = DateUtils.addMonthsToMonthDate(nowDate, -3);
  print(a);
  ///출력 2023-10-01 00:00:00.000
}

 

 

 

3. dateOnly

dateOnly(DateTime date) => DateTime

 

인자로 받은 date의 DateTime을 반환하지만 시간은 자정으로 설정됩니다.

 

*날짜는 그대로 반환하지만 시간은 자정으로 설정됨.

void main() {
  final nowDate = DateTime(2024,1,9,14,10,50);
  final DateTime a = DateUtils.dateOnly(nowDate);
  print(a);
  ///출력 2024-01-09 00:00:00.000
}

 

 

 

 

4. datesOnly

datesOnly(DateTimeRange range) => DateTimeRange

 

인자로 받은 DateTimeRange를 반환하지만 시간은 자정으로 설정됩니다.

 

*DateTime이 아닌 DateTimeRange를 반환받고 시간은 자정으로 설정됩니다.

*start에서 end까지의 범위 이기 때문에 start date가 더 크면 실행이 되지 않습니다.

void main() {
  final nowDate = DateTime(2024,1,9,14,10,50);
  final endDate = DateTime(2024,2,13,15, 30, 40);
  final DateTimeRange a = DateUtils.datesOnly(DateTimeRange(start: nowDate, end: endDate));
  print(a);
  ///출력 2024-01-09 00:00:00.000 - 2024-02-13 00:00:00.000
}

 

 

 

5. firstDayOffset

firstDayOffset(int year, int month, MaterialLocalizations localizations) => int

 

인자로 받은 year 년도의 month월의 첫 번째 요일을 int로 반환합니다.

 

* 0 = 일요일, 1 = 월요일, 2 = 화요일, 3 = 수요일, 4 = 목요일, 5 = 금요일, 6 = 토요일

* 국가에 따라 다르기 때문에 localizations를 받습니다.

* 2024년 3월의 첫 번째 요일은 금요일이라 5를 반환하는 모습

void main() {
  int a = DateUtils.firstDayOffset(2024,3,MaterialLocalizations.of(context));
  print(a);
  ///출력 5
}

 

 

 

6. getDaysInMonth

getDaysInMonth(int year, int month) => int

 

인자로 받은 year 년도의 month 월의 총 일 수를 반환합니다.

 

*2024년의 2월은 총 29일이라서 29를 반환하는 모습.

void main() {
  int a = DateUtils.getDaysInMonth(2024,2);
  print(a);
  ///출력 29
}

 

 

 

 

7. isSameDay

isSameDay(DateTime? dateA, DateTime? dateB) => bool

 

인자로 받은 dateA와 dateB의 년 / 월 / 일 을 비교해서 같으면 true, 다르면 false를 반환합니다.

 

* 년 / 월 / 일 만 비교하기 때문에 true가 반환

void main() {
  final nowDate = DateTime(2024,2,13,14,10,50);
  final endDate = DateTime(2024,2,13,15, 30, 40);
  final bool a = DateUtils.isSameDay(nowDate, endDate);
  print(a);
  ///출력 true
}

 

* 년 / 월 / 일 이 다르기 때문에 false 반환

void main() {
  final nowDate = DateTime(2024,2,13,14,10,50);
  final endDate = DateTime(2024,3,15,15, 30, 40);
  final bool a = DateUtils.isSameDay(nowDate, endDate);
  print(a);
  ///출력 false
}

 

* isSameDay는 인자로 null을 허용합니다.

* 어쨌든 다르기 때문에 false를 반환합니다.

void main() {
  final nowDate = DateTime(2024,2,13,14,10,50);
  final bool a = DateUtils.isSameDay(nowDate, null);
  print(a);
  ///출력 false
}

 

 

* 두 인자가 null 이면 true를 반환합니다.

void main() {
  final bool a = DateUtils.isSameDay(null, null);
  print(a);
  ///출력 true
}

 

 

 

8. isSameMonth

isSameMonth(DateTime? dateA, DateTime? dateB) => bool

 

isSameDay와 비슷하지만 isSameMonth는 년 / 월 만 비교 대상입니다.

 

* 년 / 월 만 비교하기 때문에 true를 반환

* 나머지는 isSameDay와 동일 

void main() {
  final nowDate = DateTime(2024,3,13,14,10,50);
  final endDate = DateTime(2024,3,18,15, 30, 40);
  final bool a = DateUtils.isSameMonth(nowDate, endDate);
  print(a);
  ///출력 true
}

 

 

 

 

9. monthDelta

monthDelta(DateTime startDate, DateTime endDate) → int

 

인자로 받은 두 date의 차이 월 수를 구합니다.

 

* 두 날짜의 차이 월 수는 5

void main() {
  final nowDate = DateTime(2024,3,13,14,10,50);
  final endDate = DateTime(2024,8,18,15, 30, 40);
  final int a = DateUtils.monthDelta(nowDate, endDate);
  print(a);
  ///출력 5
}

 

 

* 앞 인자가 더 높은 date면 차이 월 수가 음수로 반환됩니다.

void main() {
  final nowDate = DateTime(2024,8,13,14,10,50);
  final endDate = DateTime(2024,3,18,15, 30, 40);
  final int a = DateUtils.monthDelta(nowDate, endDate);
  print(a);
  ///출력 -5
}