How to Get Current Date in Flutter

Everyone knows the importance of dates and time in mobile apps. In this blog post, let’s learn how to get current date in flutter.

There’s a DateTime class in Dart which can be used to get current date and time.

var now = new DateTime.now();

This will give us the output as following.

2020-10-18 20:39:17.566238

Most of the time we want to show the current date in string. You can convert DateTime to string using Intl package.

You can use Intl package by adding dependency to pubspec.yaml file.

dependencies:
  flutter:
    sdk: flutter
  intl: ^0.16.1

You just need to use DateFormat class as given below.

import 'package:intl/intl.dart';
String now = DateFormat("yyyy-MM-dd hh:mm:ss").format(DateTime.now());

You will get output as given below.

2020-10-18 09:01:08

As you only want current date you can modify the format as given below.

import 'package:intl/intl.dart';
String now = DateFormat("yyyy-MM-dd").format(DateTime.now());

You will get the output in the following format.

2020-10-18

Following is the flutter example where I show current date as Text.

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

void main() => runApp(App());

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Current Date Example',
      home: DateExample(),
    );
  }
}


class DateExample extends StatelessWidget {
  const DateExample({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(child: Text(DateFormat("yyyy-MM-dd").format(DateTime.now()))),
    );
  }
}

Following is the output of flutter current date example.

If you want to know how to add a datepicker in flutter then you can check my another blog post here.

1 thought on “How to Get Current Date in Flutter”

  1. Pingback: How to Convert String to DateTime in Flutter – Flutter For You

Leave a Comment

Your email address will not be published. Required fields are marked *