Dates

Dart brings some much needed sanity to dates and times in the browser. The answer to the question that is burning in many a JavaScript refugee’s heart is “yes”—the first month is, in fact, 1. Let the rejoicing commence.

The niceties of Dart dates do not end there. For instance, there are a number of ways to create dates.

primitives/dates.dart
 
var​ mar = DateTime.parse(​'2013-03-01 14:31:12'​);
 
// 2013-03-01 14:31:12.000
 
var​ now = ​new​ DateTime.now();
 
// 2012-12-31 23:59:59.149
 
var​ apr = ​new​ DateTime(2013, 4, 1);
 
// 2013-04-01 00:00:00.000
 
var​ may = ​new​ DateTime(2013, 5, 1, 18, 18, 18);
 
// 2013-05-01 18:18:18.000

Even better, manipulating dates is not only possible but quite nice.

 
var​ jun = ​new​ DateTime(2013, 6, 1, 0, 0, 0, 0);
 
var​ jul = ​new​ DateTime(2013, 7, 1, 0, 0, 0, 0);
 
var​ diff = jul.difference(jun);
 
diff.inDays; ​// => 30
 
jul.add(​new​ Duration(days: 15)); ​// => 2013-07-16

The difference method in DateTime returns a Duration object that encapsulates a period of time. A Duration can be queried in any number of time units—from days all the way down to milliseconds. As you can see with our add example, Durations also come in handy when adding or removing time from a particular date.

Working with dates in Dart is not a thing to dread. As you can already see, they are downright pleasant.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.213.44