skip to Main Content

How to convert "/Date(1639580400000-0000)/" into the date time format in flutter dart..

"startDateTime": "/Date(1639580400000-0000)/",

i want to change this string into the date format
: exp- (12-MAY-2023)

I want to convert this string into the date format in the Flutter (dart)

3

Answers


  1. You can try this function hope this will work for you

    DateTime parseStringToDate(String input) {
      final RegExp dateRegex = RegExp(r"/Date((d+)([+-]d{4}))/");
      final match = dateRegex.firstMatch(input);
    
      if (match != null) {
        final timestamp = int.parse(match.group(1)!);
        final offset = match.group(2);
        final milliseconds = timestamp + (offset == "-0000" ? 0 : int.parse(offset!));
    
        return DateTime.fromMillisecondsSinceEpoch(milliseconds, isUtc: true);
      }
    
      throw Exception("Invalid date format: $input");
    }
    

    you can checkout full code here
    https://dartpad.dev/?id=4223a468a49f24a41f0282fb990718fb

    Login or Signup to reply.
  2.     changeDateFormat(date){
      RegExp? regex = RegExp(r'd+');
      Match? match = regex.firstMatch(date);
      int timestamp = int.parse(match!.group(0).toString());
    
      DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp, isUtc: true);
      var convertedDate = DateFormat("dd-MMM-yyyy").format(dateTime);
      return convertedDate;
    }
    
    Login or Signup to reply.
  3. add this package (intl) to your pubspec.

    String formatDate() {
      String input = "/Date(1639580400000-0000)/";
      
      int startIdx = input.indexOf("(");
      int endIdx = input.indexOf(")");
      
      String time = input.substring(startIdx+1,endIdx);
    
      int index = time.indexOf("-");
    
      if (index == -1) index = time.length;
    
      String formattedDate = DateFormat('dd-MMM-yyyy').format(
          DateTime.fromMillisecondsSinceEpoch(int.parse(time.substring(0, index))));
    
      return formattedDate;
    }
    

    demo

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search