Open In App

Flutter – Convert Formatted String into DateTime Object

Last Updated : 28 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Dart, there is 1 datatype called DateTime which refers to date and time. It has multiple inbuilt functions which provide us with various details like current date and time. Today we will learn how to convert a formatted string into a DateTime object of dart. For our convenience, we are changing the format of DateTime. So that we can better understand the date, month, year, etc. It is easy to convert a DateTime object into a string but a little difficult to convert a string into a DateTime object

Note: You cannot convert any string into data types it must have something related to date and time.

Let us understand how to convert strings into DateTime datatypes. We have 3 methods to do it

Method 1: From Inbuilt Method

Here’s some example of how you can use the inbuilt method.

Dart




void main() {
  // DateTime.parse("your_string_you_want_To_change")
  // Example 1:
  print(DateTime.parse("2012-02-27 13:27:00"));
  // Example 2:
  print(DateTime.parse("20230811"));
}


Output:

2012-02-27 13:27:00.000
2023-08-11 00:00:00.000

String which you can convert from above function

  1. “2012-02-28 13:27:00”
  2. “2012-02-27 13:27:00.123456789z”
  3. “2012-02-27 13:27:00,123456789z”
  4. “20120227 13:27:00”
  5. “20120227T132700”
  6. “20120227”
  7. “+20120227”
  8. “2012-02-27T14Z”
  9. “2012-02-27T14+00:00”
  10. “-123450101 00:00:00 Z”: in the year -12345.
  11. “2002-02-27T14:00:00-0500”:
  12. “2002-02-27T19:00:00Z”

Disadvantage:

You can convert only limited number of formatted string from this.

Method 2: Get datetime from milliseconds and microseconds epoch

Dart




void main() {
  // DateTime.fromMicrosecondsSinceEpoch(interger_which_you_want_to_change_in_datetime);
  // DateTime.fromMillisecondsSinceEpoch(interger_which_you_want_to_change_in_datetime);
  // Example 1:
  print(DateTime.fromMillisecondsSinceEpoch(1691732035256));
  print(DateTime.fromMicrosecondsSinceEpoch(1691732011600000));
}


Output:

2023-08-11 11:03:55.256
2023-08-11 11:03:31.600

Disadvantage:

You can convert integer value with more than 13 digits from this.

Method 3: From importing intl package

import intl package in your project

or

Add following in pubspec.yaml file

Dart




dependencies:
  intl: ^0.18.1


It can be used to your datetime object into formatted string and also convert string to datetime. Let us learn how we can do that.

1. To convert string to datetime object

Dart




DateTime tempDate = new DateFormat("format_for_your_string_you want_to_convert").parse("datetime_string_you want_to_convert");
 
// Example 1
DateTime tempDate = new DateFormat("yyyy-MM-dd hh:mm:ss").parse("2023-08-10 08:32:05);


2. Convert Date Time to formatted string

Dart




String date = DateFormat("format_string_for_Date_time").format(datetimeobject_you_want_to convert_to_String);
 
// Example 1
String date1 = DateFormat("yyyy-MM-dd hh:mm:ss").format(DateTime.now());
 
// Output : 2023-08-11 11:03:31.000


You can learn more about it in this article: Format Dates in Flutter

Disadvantage:

You must know the format of datetime string to convert it properly.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads