Posted in

How to Get Current Date and Time on Flutter

How to Get Current Date and Time on Flutter
How to Get Current Date and Time on Flutter

Getting the current date and time is a fundamental requirement in many apps, from displaying a timestamp on a message to setting a deadline for a task. In Flutter, this is incredibly easy to do using Dart’s built-in DateTime class.

This guide will walk you through getting the current time and, just as importantly, formatting it into a user-friendly string.

1. Getting the Current DateTime Object

To get the current date and time, you simply use the DateTime.now() constructor. This returns a DateTime object containing all the information about the current moment, right down to the microsecond.

Code Example

void main() {
  // Get the current date and time
  final DateTime now = DateTime.now();
  
  print(now);
  // Example Output: 2025-07-05 17:31:20.123
}
Dart

While this gives you the data, the default format isn’t very user-friendly. For that, we need to format it.

2. Formatting the Date and Time

The best way to format a DateTime object is with the official intl package. It provides a powerful and flexible DateFormat class for this purpose.

Step A: Add the intl Package

First, add the intl package to your pubspec.yaml file:

YAML

dependencies:
  flutter:
    sdk: flutter
  intl: ^0.19.0 # Use the latest version
Dart

Don’t forget to run flutter pub get in your terminal to install it.

Step B: Use DateFormat to Format Your Date

Now you can import the package and use DateFormat to create any format you need.

Code Example

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

class DateTimeScreen extends StatelessWidget {
  const DateTimeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    // 1. Get the current DateTime
    final now = DateTime.now();

    // 2. Create different formatters
    final formatterDate = DateFormat('yyyy-MM-dd');
    final formatterTime = DateFormat('h:mm a');
    final formatterFull = DateFormat('EEEE, MMMM d, y');

    // 3. Format the DateTime
    final formattedDate = formatterDate.format(now); // 2025-07-05
    final formattedTime = formatterTime.format(now); // 5:31 PM
    final formattedFull = formatterFull.format(now); // Saturday, July 5, 2025

    return Scaffold(
      appBar: AppBar(
        title: const Text('Date and Time'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              'Date: $formattedDate',
              style: const TextStyle(fontSize: 24),
            ),
            Text(
              'Time: $formattedTime',
              style: const TextStyle(fontSize: 24),
            ),
            const SizedBox(height: 20),
            Text(
              formattedFull,
              style: const TextStyle(fontSize: 20),
            ),
          ],
        ),
      ),
    );
  }
}
Dart

You can find a full list of formatting patterns in the intl package documentation.

3. Accessing Individual Date and Time Components

If you need to perform logic based on a specific part of the date (like the year or month), you can access these components directly from the DateTime object as properties.

final now = DateTime.now();

int year = now.year;       // 2025
int month = now.month;      // 7 (for July)
int day = now.day;        // 5
int hour = now.hour;       // 17 (24-hour format)
int minute = now.minute;    // 31
int weekday = now.weekday;  // 6 (where Saturday is 6)

// Example usage
if (now.weekday == DateTime.saturday) {
  print("It's the weekend!");
}
Dart

Key Takeaways

  • Get the object: Use DateTime.now() to get the current moment.
  • Format for users: Use the intl package and its DateFormat class to convert the DateTime object into a human-readable string.
  • Access for logic: Use properties like .year, .month, and .weekday for conditional logic.

Flutter Stuff Is A Team Of Passionate Flutter Developers On A Mission To Empower The Community. We Share Our Expertise And Insights Through Comprehensive Guides, Tutorials, And Resources, Making Flutter Mobile App Development Accessible And Enjoyable For Everyone.

Leave a Reply