Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i format date like this 2019-07-08T10:37:28Z in flutter

I want to format the date like this 2019-07-08T10:37:28Z but I don't know how to do it can anyone help me with this.

Here is the code for date picker

final format = new DateFormat.yMMMEd('en-US');

return DateTimeField(
      format: format,
      autocorrect: true,
      autovalidate: false,
      controller: _bspLicenseExpiryDate,
      readOnly: true,
      validator: (date) => date == null ? 'Please enter valid date' : null,
      decoration: InputDecoration(
          labelText: "Expiry Date",
          hintText: "Expiry Date",
          prefixIcon: Icon(
            FontAwesomeIcons.calendar,
            size: 24,
          )),
      onShowPicker: (context, currentValue) {
        return showDatePicker(
          context: context,
          firstDate: DateTime.now(),
          initialDate: currentValue ?? DateTime.now(),
          lastDate: DateTime(2100),
        );
      },
    );
like image 206
Rutvik Gumasana Avatar asked Dec 22 '22 20:12

Rutvik Gumasana


2 Answers

You can use this:

var now = new DateTime.now();
var dateFormatted = DateFormat("yyyy-MM-ddTHH:mm:ss").format(now);

You have to add the "Z" at the end of the String because is a Char used to format the TimeZone.

You can check it there -> https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html

like image 128
Caffo17 Avatar answered Dec 27 '22 10:12

Caffo17


You can refer to DateFormat class here https://api.flutter.dev/flutter/intl/DateFormat-class.html It has a constructor where you can put your pattern.

    final format = new DateFormat('yyyy-MM-ddTHH:mm:ssZ','en-US');
like image 34
Debasmita Sarkar Avatar answered Dec 27 '22 11:12

Debasmita Sarkar