Book Image

DART Cookbook

By : Ivo Balbaert
Book Image

DART Cookbook

By: Ivo Balbaert

Overview of this book

Table of Contents (18 chapters)
Dart Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Working with dates and times


Proper date-time handling is needed in almost every data context. What does Dart give us to ease working with dates and times? Dart has the excellent built-in classes DateTime and Duration in dart:core. As a few of its many uses, you can do the following:

  • Compare and calculate with date times

  • Get every part of a date-time

  • Work with different time zones

  • Measure timespans with Stopwatch

However, the DateTime class does not provide internationalization; for this purpose, you need to use the intl package from the Dart team.

How to do it...

The following are some useful techniques (try them out in date_time.dart):

  • Formatting dates (from DateTime to a string) to standard formats, but also to any format using the package intl, as shown in the following code:

    import 'package:intl/intl.dart';
    import 'package:intl/date_symbol_data_local.dart';
    
      print(now.toIso8601String()); // 2014-05-08T14:03:21.238
      print(now.toLocal());         // 2014-05-08 14:03:21.238
      print(now.toString...