Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart - How to format DateTime based on device timezone?

Tags:

flutter

dart

I am a newbie to Dart. I am currently facing a problem to format my DateTime to display the time based on the device's timezone. Can someone advise me how can I achieve that? My current code as following:

DateFormat('dd MMM yyyy hh:mm')

Output Sample: 11 Mar 2019 07:04

Expectation: My device is using GMT+8 at the moment, I am expecting it to display as 11 Mar 2019 15.04.

Please advise. Thanks!!

like image 385
user3180690 Avatar asked Dec 23 '22 00:12

user3180690


1 Answers

For converting UTC to Local Timezone :

import 'package:intl/intl.dart';

var dateValue = new DateFormat("yyyy-MM-ddTHH:mm:ssZ").parseUTC("2020-06-14T18:55:21Z").toLocal();
String formattedDate = DateFormat("dd MMM yyyy hh:mm").format(dateValue);
debugPrint("formattedDate = "+formattedDate);
like image 142
Rafi Avatar answered Jan 12 '23 00:01

Rafi