1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Flutter Time Difference

Discussão em 'Mobile' iniciado por Stack, Outubro 9, 2024 às 23:32.

  1. Stack

    Stack Membro Participativo

    // 시간차 구하기
    timeDifference() {
    // 현재 시간 time now
    DateTime now = DateTime.now();

    // 오늘의 아침 9시 09:00
    DateTime morningTime = DateTime(now.year, now.month, now.day, 9, 0, 0);

    // 시간 차이 계산 time difference
    Duration difference = now.difference(morningTime);

    // 시간과 분으로 변환
    int hours = difference.inHours;
    int minutes = difference.inMinutes.remainder(60);

    // 출력
    if (difference.isNegative) {
    print('현재 시간은 아침 9시 이전입니다.');
    } else {
    print('아침 9시와의 시간 차이는 $hours시간 $minutes분 입니다.');
    }
    }


    This code calculates the time difference between the current time and 9:00 AM of the same day.


    1. Current time is retrieved using DateTime.now().


    2. Morning time (9:00 AM) is defined using DateTime with today’s date and 9:00 AM.


    3. Time difference is calculated using now.difference(morningTime), which returns a Duration.


    4. The hours and minutes are extracted from the Duration using inHours and inMinutes.remainder(60).


    5. The code checks if the difference is negative (meaning it's before 9:00 AM) and prints the appropriate message; otherwise, it displays the time difference in hours and minutes.

    This way, you can see how long it has been since 9:00 AM or if it's before 9:00 AM.

    Continue reading...

Compartilhe esta Página