skip to Main Content

I need to convert this date format Sun Dec 24 2023 21:00:00 GMT+0000 (Coordinated Universal Time) to yyyy:MM:dd HH:mm in flutter what is the actual date format Patten i can use for convert to custom date format?

3

Answers


  1. Chosen as BEST ANSWER

    call function -> convertUniversalTimeToCustomFormat('Sun Dec 24 2023 21:00:00 GMT+0000 (Coordinated Universal Time)')

    convertUniversalTimeToCustomFormat(String dateTime, {String currentDateFormat = 'EEE MMM dd yyyy HH:mm:ss zzz', String newDateFormat = 'yyyy:mm:dd hh:mm'}) { var date = ''; try { date = DateFormat(newDateFormat).format(DateTime.parse(DateFormat(currentDateFormat).parse(dateTime).toString())); } catch (e) { date = 'Invalid date'; } return date; }


  2. Use DateFormat from intl package to format dates:

    import 'package:intl/intl.dart';
    
    void main() {
      String dateString = "Sun Dec 24 2023 21:00:00 GMT+0000 (Coordinated Universal Time)";
    
      /// Parsing the dateString to DateTime
      DateTime originalDate =
          DateFormat("EEE MMM dd y HH:mm:ss").parse(dateString);
    
      /// Getting the Formatted Date String 
      String formattedDate = DateFormat('yyyy:MM:dd HH:mm').format(originalDate);
    
      print(formattedDate); 
    }
    
    Login or Signup to reply.
  3. First, make sure to include the intl package in your pubspec.yaml file:
    dependencies:

    flutter:
    sdk: flutter intl: ^0.19.0

    You can the below code as per need

    String dateString = "Sun Dec 24 2023 21:00:00 GMT+0000 (Coordinated Universal Time)";

    // Parse the original date string
    DateTime originalDate = DateTime.parse(dateString);
    
    // Create a date format pattern
    DateFormat dateFormat = DateFormat('yyyy-MM-dd HH:mm');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search