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

 Convert timestamp string from 24 hours to 12-hour format in Dart and Flutter

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. The DateTime.parse() method creates a DateTime object using a 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

Perl How to: Find a Given Variable String is numeric or not

Dart/Flutter: Check if String is Empty, Null, or Blank example

How to generate Unique Id UUID in Dart or Flutter Programming| Dart or Flutterby Example

How to Sort List of numbers or String in ascending and descending in Dart or Flutter example

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

Dart Example - Program to check Leap year or not

Dart Example to count the number of digits in a number| Decimals Count in Dart