-1

all

In flutter dart,

I need chage "Oct 20, 2020 7:47:31 PM" to datetime,

what is time format of "Oct 20, 2020 7:47:31 PM".

Thanks.

dongdong
  • 77
  • 1
  • 9
  • Does this answer your question? [convert datetime string to datetime object in dart?](https://stackoverflow.com/questions/49385303/convert-datetime-string-to-datetime-object-in-dart) – jamesdlin Oct 21 '20 at 08:55
  • Thanks jamesdlin, My string is "Oct 20, 2020 7:47:31 PM",can not use DateTime.parse. – dongdong Oct 24 '20 at 04:45
  • Did you read the other answers to that question? – jamesdlin Oct 24 '20 at 04:50

3 Answers3

0

You can try this DateFormat:

DateFormat dateFormat = DateFormat('MMM dd, yyyy h:mm:ss a');
dangngocduc
  • 1,588
  • 8
  • 13
0

You can use this package https://pub.dev/packages/intl

import 'package:intl/intl.dart';

class Sample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
          alignment: Alignment.center,
          child: Text(DateFormat('MMM dd, yyyy h:mm:ss a')
              .format(DateTime.parse(DateTime.now().toString())))),
    );
  }
}
Raine Dale Holgado
  • 2,932
  • 2
  • 17
  • 32
  • Thasks Reign,I used: DateTime dt=DateFormat('MMM dd, yyyy h:mm:ss a').parse(oldtime); but error:Trying to read MMM from Oct 24, 2020 12:43:54 PM at position 0. how i can do? – dongdong Oct 24 '20 at 04:46
0

Try this one

DateTime dateTime = DateTime.now();
var finalDate = DateFormat('yyyy-MM-dd HH:mm:ss').format(dateTime);
AegisX1
  • 75
  • 1
  • 8
  • if u want to make it in different read this : https://api.flutter.dev/flutter/intl/DateFormat-class.html – AegisX1 Oct 21 '20 at 05:51
  • Thanks Nervous,has a error: DateTime dt=DateFormat('MMM dd, yyyy h:mm:ss a').parse(oldtime); – dongdong Oct 24 '20 at 04:48