Home > Back-end >  Replace hour with 12am in date format
Replace hour with 12am in date format

Time:01-30

I have this code:

import 'package:intl/intl.dart';

void main() {
  String date = "31-01-2023";
  String hour = "12:30";
  
  String completeData = "${date} ${hour}";
  String tempHour = hour.replaceAll(":", ".");
  DateTime tempDate = new DateFormat("dd-MM-yyyy hh:mm").parse(completeData);
  print("Formatting date: ${tempDate}");
  
}

When I print the Formatting date:

2023-01-31 00:00:30.000

I replace the 12 with 00, but I need to keep 12.

If I pass 11,13,14 or more it works correctly

CodePudding user response:

Here is an excerpt from the intl documentation:

/// The following characters are available in explicit patterns:
///
///     Symbol   Meaning                Presentation       Example
///     ------   -------                ------------       -------
///     ...
///     h        hour in am/pm (1~12)   (Number)           12
///     H        hour in day (0~23)     (Number)           0
///     ...

Since you are using hh:mm pattern, the 12:30 gets converted to 00:30. What you can do, is use this pattern dd-MM-yyyy HH:mm - it should work.

  • Related