Develop/Flutter

[Flutter / Dart 플러터] 날짜 더하기 빼기 계산하기

issuemaker99 2024. 8. 17. 19:20
728x90

▶ 날짜 더하기 

예제 소스 

void main() {
    DateTime today = DateTime.now();
    // DateTime dt = DateTime(2024, 8, 17);
    print('DateTime: $today');

    DateTime dt2 = today.add(Duration(days: 1));
    print(dt2);

    DateTime dt3 = today.add(Duration(hours: 2));
    print(dt3);

    DateTime dt4 = today.add(Duration(minutes: 3));
    print(dt4);

    DateTime dt5 = today.add(Duration(days: 1, hours: 2, minutes: 3));
    print(dt5);
}

 

결과 

DateTime: 2024-08-17 19:15:01.894
2024-08-18 19:15:01.894
2024-08-17 21:15:01.894
2024-08-17 19:18:01.894
2024-08-18 21:18:01.894

 

▶ 날짜 빼기 

예제소스

void main() {
    DateTime today = DateTime.now();
    // DateTime dt = DateTime(2024, 8, 17);
    print('DateTime: $today');

    DateTime dt2 = today.subtract(Duration(days: 1));
    print(dt2);

    DateTime dt3 = today.subtract(Duration(hours: 2));
    print(dt3);

    DateTime dt4 = today.subtract(Duration(minutes: 3));
    print(dt4);

    DateTime dt5 = today.subtract(Duration(days: 1, hours: 2, minutes: 3));
    print(dt5);
}

 

결과 

DateTime: 2024-08-17 19:18:06.697
2024-08-16 19:18:06.697
2024-08-17 17:18:06.697
2024-08-17 19:15:06.697
2024-08-16 17:15:06.697
LIST