1

I am parsing a date in format yyyy-MM-dd HH:MM:SS using DateFormat

But I am getting some other date in parsed output

final dt = '2022-02-07 05:00:11';
final datm =  DateFormat('yyyy-MM-dd HH:MM:SS').parse(dt);

print ("DATE_CALC ${datm.day} - ${datm.month} - ${datm.year} ${datm.hour} : ${datm.minute} : ${datm.second}   "); 

Actual Output:

DATE_CALC 7 - 2 - 2021 5 : 0 : 0 

Expected :

DATE_CALC 07 - 12 - 2022 05 : 00 : 11 

Why I am getting some wrong date ? Am I doing anything wrong ?
RagAnt
  • 1,064
  • 2
  • 17
  • 35

4 Answers4

4

MM is not month and minutes.... mm or MM :-)

wrong 'yyyy-MM-dd HH:MM:SS'
correct 'yyyy-MM-dd HH:mm:ss'
Maurice Raguse
  • 4,437
  • 2
  • 29
  • 46
1
final dt = '2022-02-07 05:00:11';
final datm =  DateFormat("yyyy-MM-dd HH:mm:ss").parse(dt);
  print ("DATE_CALC ${datm.day} - ${datm.month} - ${datm.year} ${datm.hour} : ${datm.minute} : ${datm.second}   ");

Try this Hour minute and seconds can be parsed like this HH:mm:ss not like HH:MM:SS

Balaji
  • 1,773
  • 1
  • 17
  • 30
  • Working . Thanks . One help. Is it possible to print with leading zeros like this 07 - 02 - 2022 5 : 00 : 11 ? – RagAnt Feb 10 '22 at 13:11
  • print ("DATE_CALC ${datm.day.toString().padLeft(2, '0')}"); try this It will print `DATE_CALC 07` – Balaji Feb 10 '22 at 13:17
1

Your formate is wrong

yyyy-MM-dd HH:MM:SS replace to 'yyyy-MM-dd HH:mm:ss'

Anmol Mishra
  • 322
  • 3
  • 7
1

it should be final datm = DateFormat('yyyy-MM-dd HH:mm:ss').parse(dt);

Hardik Mehta
  • 2,195
  • 1
  • 11
  • 14
  • Working . Thanks . One help. Is it possible to print with leading zeros like this 07 - 02 - 2022 5 : 00 : 11 ? – RagAnt Feb 10 '22 at 13:11
  • 1
    you can try this : print ("DATE_CALC ${datm.day.toString().padLeft(2, '0')} - ${datm.month.toString().padLeft(2, '0')} - ${datm.year} ${datm.hour} : ${datm.minute} : ${datm.second} "); – Hardik Mehta Feb 10 '22 at 13:23