Flutter – How to Change Date and Time Format

dartflutter

how are you ?

my backend just accept date and time in this format 2019-03-24 11:00:00

and i use in my app calendar widget to get the date and it print the date in this format 2019-04-24 12:00:00.000Z and i use this code to get the time

TimeOfDay _time = new TimeOfDay.now();

  Future<Null> _selecTime(BuildContext context) async {
    final TimeOfDay picked = await showTimePicker(
      context: context,
      initialTime: _time,
    );
    if (picked != null && picked != _time) {
      print('${_time.toString()}');
      setState(() {
        _time = picked;
      });
    }
  }

and i get this TimeOfDay(07:37)

so how can i get format for date like this 2019-04-24 and for time like this 11:00:00 and i will deal with it

thanks

Best Answer

You can use intl package

Here is an example:

import 'package:intl/intl.dart';

main() {
  var now = new DateTime.now();
  var dateFormat = new DateFormat('yyyy-MM-dd HH:mm:ss');
  String formattedDate = dateFormat.format(now);
  print(formattedDate); // 2019-04-28 09:02:29
}
Related Question