Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Change this "2022-07-26T12:10:07.000+0000" format to DateTime with 12 hrs format? Flutter

How i can extract the Time alone in this format "2022-07-26T12:10:07.000+0000" and show in 12hrs.

The output i am supposed to get is "5:40 PM", but i am getting 12.10 AM.

How to get exact time in the above mentioned format?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is the method i followed to parse the DateTime.

 String getTimeStringNew(String date) {
 DateFormat dateFormat = DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+SSSS");
 var dateValue = dateFormat.parse(date);
 DateFormat dateFormat1 = DateFormat("hh:mm a");
 var value = dateFormat1.format(dateValue);
 return value;}

 

In this method i am getting "12:10 AM".

But correct time is "5.40 PM".

What is the mistake i am doing here. Please correct.

>Solution :

2022-07-26T12:10:07.000+0000

Here 2022 is year

07 is month

26 is day

12 is hour

10 is minutes

07 is seconds.

So you will get 12.10 only

Maybe you are recieving the time in UTC.

You can use .toLocal to convert it to local time

var dateLocal = dateUtc.toLocal();

Edit

String getTimeStringNew(String date) {
 DateTime _localDate = DateTime.parse(date).toLocal();

 DateFormat dateFormat = DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+SSSS");
 var dateValue = dateFormat.format(_localDate);
 DateFormat dateFormat1 = DateFormat("hh:mm a");
 var value = dateFormat1.format(_localDate);
 return value;}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading