Ir para conteúdo

Bem vindo ao Fluttership

Fluttership

Entre para seguir isso  
Alex Castro

Diferença entre datas

Recommended Posts

void main() {
  DateTime hoje = DateTime.parse('2019-09-30');
  DateTime ontem = DateTime.parse('2019-09-29');
  
  if(hoje.difference(ontem).inDays > 0) {
    print('$hoje é maior que $ontem');
  }
  
}

Este é um modo para comparar se uma data é maior que outra...

 

Entre outras:

 

1. add()

This adds the provided duration and returns a new DateTime instance:

var berlinWallAdd10 = berlinWallFell.add(Duration(days: 10, hours: 5))); // 19th of November at 05:00 hrs

print(berlinWallAdd10.day); // => 19
print(berlinWallAdd10.hour); // => 5

2. difference()

This accepts another DateTime object, returning the difference as a Duration object. You can then extract the days, hours, minutes and so on.

var diff = berlinWallFell.difference(moonLanding);

diff.inDays; // => 7416 - log these out with print(diff.inDays);
diff.inHours; // => 177987
diff.inMinutes; // => 10679261

3. isAfter(DateTime other)

This checks that the given date is after other:

moonLanding.isAfter(berlinWallFell); // => false

4. isBefore(DateTime other)

This checks that the given date is before other:

moonLanding.isBefore(berlinWallFell); // => true

5. compareTo(DateTime other)

Checks to see that the date values are equal. Returns 0 if they are.

berlinWallFell.compareTo(berlinWallFell); // => 0 (equal)
moonLanding.compareTo(berlinWallFell); // => -1 (not equal)

6. subtract(Duration duration)

Subtracts the given duration from the date.

berlinWallFell.subtract(
  Duration(days: 7416, hours: 3, minutes: 41, seconds: 56)); // => 1969-07-20 20:18:04.000Z (about the day and time of the moon landing)

7. toLocal()

Returns the date in the local time zone. Useful for i18n.

moonLanding.toLocal(); // => 1969-07-20 21:18:04.000

8. toUtc()

Returns the date in UTC time.

moonLanding.toUtc(); // => 1969-07-20 20:18:04.000Z
moonLanding.timeZoneName; // => UTC

Compartilhar este post


Link para o post
Compartilhar em outros sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Visitante
Responder

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Entre para seguir isso  

×
×
  • Criar Novo...