skip to Main Content

I have a date and time in String format and want to convert it Datetime and then in String.

I have String format as – (2023-04-06T20:34:53.981+00:00)

and I want output like – (07/04/2023 02:04:53 AM) and (Apr 7, 2023 02:04:53 AM)

So how I can do this?

Please help me.

3

Answers


  1. import 'package:intl/intl.dart';
    void main() {
        String inputString = "2023-04-06T20:34:53.981+00:00";
      
      // Convert the input string to DateTime object
      DateTime dt = DateTime.parse(inputString);
      
      // Format the DateTime object into your desired output string format
      String formattedDateTime1 = DateFormat("dd/MM/yyyy hh:mm:ss a").format(dt);
      String formattedDateTime2 = DateFormat("MMM d, yyyy hh:mm:ss a").format(dt);
      
      print(formattedDateTime1);
      print(formattedDateTime2);
    }
    

    And I’d suggest you to google these things first and then come to StackOverflow, read the docummentation on intl library.

    Login or Signup to reply.
  2. try the below code .. to formate the date time you have to use intl package

    import 'package:intl/intl.dart';
    var datetime=DateTime.parse('2023-04-06T20:34:53.981+00:00');
    var formateddate= DateFormat("MMM d, yyyy hh:mm:ss a").format(datetime);
    

    and yes you can find it just by google search

    Login or Signup to reply.
  3. You can use the intl package to format dates and times. Here’s an example code snippet that shows how to convert the given string to a DateTime object and then format it into the desired string formats:

    import 'package:intl/intl.dart';
    
    void dateTimeConversion() {
      String dateString = '2023-04-06T20:34:53.981+00:00';
      DateTime dateTime = DateTime.parse(dateString);
    
      // format 1: 06/04/2023 02:04:53 AM
      String formattedDateTime1 = DateFormat('dd/MM/yyyy hh:mm:ss a').format(dateTime);
      print(formattedDateTime1);
    
      // format 2: Apr 6, 2023 02:04:53 AM
      String formattedDateTime2 = DateFormat('MMM d, yyyy hh:mm:ss a').format(dateTime);
      print(formattedDateTime2);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search