Skip to content

Instantly share code, notes, and snippets.

@graphicbeacon
Created September 13, 2018 19:27
Show Gist options
  • Save graphicbeacon/e37bd9e9b9181fb9fb50b317d7ddf6af to your computer and use it in GitHub Desktop.
Save graphicbeacon/e37bd9e9b9181fb9fb50b317d7ddf6af to your computer and use it in GitHub Desktop.
Sample snippet for "Top 8 Date methods you should know (Dart)" on Dev.to
void main() {
var now = new DateTime.now();
var berlinWallFell = new DateTime.utc(1989, 11, 9);
var moonLanding = DateTime.parse('1969-07-20 20:18:04Z');
print(now);
print(berlinWallFell);
print(moonLanding);
print('\n---\n');
// DateTime constants
print(berlinWallFell.weekday == DateTime.thursday);
print(moonLanding.weekday == DateTime.sunday);
print(moonLanding.month == DateTime.july);
print(DateTime.daysPerWeek);
print('\n---\n');
// add()
var berlinWallAdd10 = berlinWallFell.add(Duration(days: 10, hours: 5));
print(berlinWallAdd10.day);
print(berlinWallAdd10.hour);
print('\n---\n');
// difference()
var diff = berlinWallFell.difference(moonLanding);
print(diff.inDays);
print(diff.inHours);
print(diff.inMinutes);
print('\n---\n');
// isAfter()
print(moonLanding.isAfter(berlinWallFell));
print('\n---\n');
// isBefore()
print(moonLanding.isBefore(berlinWallFell));
print('\n---\n');
// compareTo()
print(berlinWallFell.compareTo(berlinWallFell));
print(moonLanding.compareTo(berlinWallFell));
print('\n---\n');
// subtract()
print(berlinWallFell.subtract(
Duration(days: 7416, hours: 3, minutes: 41, seconds: 56)
));
print('\n---\n');
// toLocal()
print(moonLanding.toLocal());
print('\n---\n');
// toUtc()
print(moonLanding.toUtc());
print(moonLanding.timeZoneName);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment