Converting a date into a string

Dart:
Converting a date into a string

How to:

Dart provides the DateTime class for handling dates and times, and the intl package for formatting. First, ensure you have the intl package by adding intl: ^0.17.0 (or the latest version) to your pubspec.yaml file.

Using Dart’s Core Library

DateTime now = DateTime.now();
String formattedDate = "${now.year}-${now.month}-${now.day}";
print(formattedDate); // Output: 2023-4-12 (for example, this depends on the current date)

This example directly constructs a string from the DateTime’s properties.

Using intl package

First, import the package:

import 'package:intl/intl.dart';

Then, format the date:

DateTime now = DateTime.now();
String formattedDate = DateFormat('yyyy-MM-dd').format(now);
print(formattedDate); // Output: 2023-04-12

The intl package allows for much more complex formatting easily, including locale-specific formats:

String formattedDateLocale = DateFormat.yMMMMd('en_US').format(now);
print(formattedDateLocale); // Output: April 12, 2023

These examples show simple but powerful ways to convert and format dates into strings in Dart, either using Dart’s core functionality or utilizing the intl package for more advanced formatting options.