skip to Main Content

I have a string showing the date and time, but I only want to see the hour and minute. how do i do this with flutter.Sorry if there are other questions that I missed.

String? time = "2023-02-21 14:50:40";

Result:

String? time = "14:50";

I tried to try with regex but failed

2

Answers


  1. I think the quickest way is using the built-in DateTime.parse():

      final dt = DateTime.parse('2023-02-21 14:50:40');
      final result = '${dt.hour}:${dt.minute}';
    
    Login or Signup to reply.
  2. import 'package:intl/intl.dart';
    void main() {
    String? time = "2023-02-21 14:50:40";
    DateTime tempDate =  DateFormat("yyyy-MM-dd hh:mm:ss").parse(time);
    String? hourTime = tempDate.hour.toString();
    String? minuteTime = tempDate.minute.toString();
    print(hourTime); //14
    print(minuteTime); //50
    }
    

    You can use intl package for convert string to datetime.

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