Flutter/Dart Extract just the Date and Time?

1,048

Solution 1

you can use this code:

DateTime x = DateTime.parse("2021-08-11T11:38:09.000Z");

then create a function to check the input number:

String checkNum(int x){
   return x<10?"0${x}":x.toString();
}

now you can use this function as follow:

  print(checkNum(x.year));
  print(checkNum(x.month)); // and so on...

let me know if you had any related problems. here is an example: enter image description here.

Solution 2

Since the provided timestamp is supported by DateTime.parse you could do something like this:

void main() {
  final dateTime = DateTime.parse('2021-08-11T11:38:09.000Z');
  print(convertDateTimeToString(dateTime)); // 2021-08-11 11:38
}

String convertDateTimeToString(DateTime dt) => '${dt.year}-'
    '${dt.month.toString().padLeft(2, '0')}-'
    '${dt.day.toString().padLeft(2, '0')} '
    '${dt.hour.toString().padLeft(2, '0')}:'
    '${dt.minute.toString().padLeft(2, '0')}';

Alternative, you can use a package like intl which supports formatting DateTime using a formatting pattern:

import 'package:intl/intl.dart';

void main() {
  final dateTime = DateTime.parse('2021-08-11T11:38:09.000Z');
  final format = DateFormat('yyyy-MM-dd HH:mm');
  print(format.format(dateTime)); // 2021-08-11 11:38
}
Share:
1,048
Meggy
Author by

Meggy

I'm a self-taught novice stumbling like a drunk through php, javascript, mysql, drupal and flutter.

Updated on December 31, 2022

Comments

  • Meggy
    Meggy over 1 year

    How would I either reformat, convert or extract just the date and time from this string in Dart/Flutter? The String comes from the default timestamp in my MySQL database;

    2021-08-11T11:38:09.000Z
    

    So what I need is something like this;

      2021-08-11 11:38
    

    I was thinking perhaps something like;

    final String? shortime = timestamp?.replaceAll(RegExp(''), '') ?? ''; 
    

    But I'm not sure what to use as the regex.

  • julemand101
    julemand101 over 2 years
    Did you try the solution? It does not take into account that the date and time should be padded with 0 to the number is always two digits.
  • Abbasihsn
    Abbasihsn over 2 years
    I will update my answer, thank you.
  • Meggy
    Meggy over 2 years
    I actually used your first answer. The edited one gave this error; A value of type 'Object' can't be returned from the function 'checkNum' because it has a return type of 'String?'.
  • Abbasihsn
    Abbasihsn over 2 years
    @Meggy I edited my answer again (just missed () in front of toString!). Now you can have access to each parameter you want like (x.hour, x.minute, and so on), also checkNum function checks that return String always have two digits for example it returns "01" instead of "1"
  • Abbasihsn
    Abbasihsn over 2 years
    @Meggy did it work?
  • Meggy
    Meggy over 2 years
    I used your first answer. Android Studio gave this error on the function; A value of type 'Object' can't be returned from the function 'checkNum' because it has a return type of 'String?
  • Abbasihsn
    Abbasihsn over 2 years
    @meggy did you tried replacing x.toString with x.toString()? In checkNum function?
  • Meggy
    Meggy over 2 years
    I think the problem is that x is not an integer.
  • Abbasihsn
    Abbasihsn over 2 years
    @Meggy I checked it again, it works in the dartpad. dartpad.dev/?null_safety=true
  • Meggy
    Meggy over 2 years
    I'm not sure why but it didn't work in Android studio. At any rate your first answer worked.
  • Abbasihsn
    Abbasihsn over 2 years
    fine... so I think you can neglect numCheck function and do not use it... Could you use it as an acceptable solution?