skip to Main Content

I’m getting the string "enddate" from my django backend api in the following format :

"enddate" : "2024-08-24T05:30:00+05:30"

My purpose is to convert it to Datetime in my flutter app in the following forma:

dd/MM/yyyy  i.e 24/08/2024

so that it becomes easy to read for the users.

Based on the several stack posts i have read,I have figured the following function but I cant seem to figure to use DateFormat function in the correct manner for the input date. Here is what i have tried :

DateTime parseEndDate(String enddate) {
    var inputFormat = DateFormat('yy/MM/dd');
    var inputDate = inputFormat.parse(enddate.toString()); // <--yy/MM/dd format

    var outputFormat = DateFormat('dd/MM/yyyy');
    var outputDate = outputFormat.format(inputDate); // <-- dd/MM/yy format

    return DateTime.parse(outputDate).toLocal();
  }

4

Answers


  1. Rewrite the function such that it will parse the date string to DateTime and return the String of your desired format

    String parseEndDate(String enddate) {
        var inputDate = DateTime.parse(enddate);
        var outputFormat = DateFormat('dd/MM/yyyy');
        return outputFormat.format(inputDate);
    }
    
    Login or Signup to reply.
  2. Try below code:

    With Package – intl

     String withPackage(String enddate) {
        var inputDate = DateTime.parse(enddate);
        return DateFormat('dd/MM/yyyy').format(inputDate);
      }
    
    print('With Package - ${withPackage('2024-08-24T05:30:00+05:30')}');
    

    With out Package

    String withoutPackage(String date) {
        DateTime parsedDate = DateTime.parse(date);
    
        String day = parsedDate.day.toString().padLeft(2, '0');
        String month = parsedDate.month.toString().padLeft(2, '0');
        String year = parsedDate.year.toString();
        return "$day/$month/$year"; // merge your expected format like dd/MM/yyyy
        }
    
    print('With out Package - ${withoutPackage('2024-08-24T05:30:00+05:30')}');
    
    Login or Signup to reply.
  3. Your parseEndDate function has the following two issues:

    • The var inputFormat = DateFormat('yy/MM/dd'); is incorrect because the format of the enddate string is yyyy-MM-dd.
    • The outputFormat (dd/MM/yyyy) cannot be repeatedly parsed to a DateTime object.

    You can write the following string extension functions to get either a formatted date string or a DateTime object:

    import 'package:intl/intl.dart';
    
    extension DateParsing on String {
      String toDateString({String format = 'dd/MM/yyyy'}) {
        final inputDate = DateTime.parse(this);
        return DateFormat(format).format(inputDate);
      }
      
      DateTime toDateObject() {
        return DateTime.parse(this);
      }
    }
    

    Then use it like this:

    '2024-08-24T05:30:00+05:30'.toDateString(); //returns a formatted date string
    '2024-08-24T05:30:00+05:30'.toDateObject(); //returns a DateTime object
    //or you can also parse to a different format
    '2024-08-24T05:30:00+05:30'.toDateString(format: 'dd-MM-yyyy');
    
    Login or Signup to reply.
  4. Your returned datetime is in ISO 8601 format, so Flutter can easy to parse it to datetime object using DateTime.parse() method. This method automatically handles the ISO 8601 format, including the timezone offset.

    String dateTimeString = "2024-08-24T05:30:00+05:30";
    DateTime dateTime = DateTime.parse(dateTimeString);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search