Dart| Flutter: How to Convert timestamp string from 24 hours to 12 hour format| Flutter By Example

By Default, the DateTime object returns the date and time, time contains a 24-hour format.

This tutorial shows how to convert the String of date and time into a DateTime class object in dart and flutter.

How to convert time string from 24 hours to 12 hour format in Dart

For example, a String contains a date formatted string - 2022-05-20 23:12:20.000. DateTime.parse() method creates DateTime object using formatted string.

DateTime parse(String formattedString)

formattedString is an ISO string format.

Next, use intl dependency

First, Add the intl dependency to pubspec.yaml

    intl: any

Next, Import the intl package in the dart code file

import 'package:intl/intl.dart';

Create a DateFormat with 12 hour format from the intl package. Here is an example program

import 'package:intl/intl.dart';

void main() {
  var strDate = '2022-05-20 23:12:20.000';
  try {
    final dateFormat = DateFormat('h:mm a');

    final stringFormat = dateFormat.format(DateTime.parse(strDate));
  } on Exception catch (exception) {


11:12 PM
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.

Related posts

How to iterate loop Enum in Dart | Get Enum with index in Flutter By Example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart tutorial examples/ Flutter By Examples

How to add Methods or Values to Enums in Dart | Flutter By Example

How to convert Double to Integer or Integer to double in Dart| Flutter By Example

How to: Enum to String in Dart | Print Enum name as String in Flutter By Example

Dart| Flutter How to get File name and extension with example