I would like to know how a date such as "2022-07-17T01:46:12.632892 05:30"
be converted to a Human Readable date in DD/MM/YYYY
and hh:mm:ss
format? I probably have not surfed through a lot of other questions and suggestions on the Internet but the ones I came across were not of any help. Also, what are such date formats(like the one in question) called?
CodePudding user response:
It is rather straightforward using DateFormat
from the package intl
which comes with Flutter:
import 'package:intl/intl.dart';
void main() {
final dateTime = DateTime.parse('2022-07-17T01:46:12.632892 05:30').toUtc();
final dateFormat = DateFormat('dd/MM/yyyy HH:mm:ss');
print(dateFormat.format(dateTime)); // 16/07/2022 20:16:12
}
The time has here been converted to UTC to make the example the same for all readers. If not, the created DateTime
would be localtime which uses the timezone on the device which the program are running.
If you want to print the time using the timezone offset of 5 hours and 30 minutes, you can do something like this:
import 'package:intl/intl.dart';
void main() {
final dateTime = DateTime.parse('2022-07-17T01:46:12.632892 05:30').toUtc();
final dateFormat = DateFormat('dd/MM/yyyy HH:mm:ss');
print(dateFormat.format(dateTime.add(const Duration(hours: 5, minutes: 30))));
// 17/07/2022 01:46:12
}