aufa
aufa

Reputation: 295

Remove date in flutter

How to remove the date but still keep the time?

Here is the date and time code:

ChatScreen.dart

 void initState() {
    super.initState();
    ConversationModel objConversation = ConversationModel(
       date: conversation!.date,
    );
 }

 @override
 Widget build(BuildContext context) {
    ...
    subtitle: Text(
          conversation!.date!,
          style: TextStyle(color: Colors.white.withOpacity(.7), fontSize: 12),),
    ...
 }

ConversationModel.dart

 class ConversationModel {
    String? date;

    ConversationModel(
     {
        this.date,
     });

    ConversationModel.fromJson(Map<String, dynamic> json) {
       date = json['date'];
    }

    Map<String, dynamic> toJson() {
       final Map<String, dynamic> data = new Map<String, dynamic>();
       data['date'] = this.date;
       return data;
   }
 

And the output:

enter image description here

Any solutions?

Upvotes: 2

Views: 428

Answers (2)

Mahdi Dahouei
Mahdi Dahouei

Reputation: 1941

you should use intl package to format DateTime. for getting the time only you can do something like this:

import 'package:intl/intl.dart';

// ...
Text(DateFormat.Hm().format(date)), // 14:20
Text(DateFormat.Hms().format(date)), // 14:20:00
// ...

Upvotes: 0

Josteve Adekanbi
Josteve Adekanbi

Reputation: 12673

Add this to your model

String getOnlyTime(){
 var time = DateTime.parse(date);
 return "${time.hour}:${time.minute}";
}

Like so:

class ConversationModel {
    String? date;

    ConversationModel(
     {
        this.date,
     });

    ConversationModel.fromJson(Map<String, dynamic> json) {
       date = json['date'];
    }

    Map<String, dynamic> toJson() {
       final Map<String, dynamic> data = new Map<String, dynamic>();
       data['date'] = this.date;
       return data;
   }

   String getOnlyTime(){
     var time = DateTime.parse(date!);
     return "${time.hour}:${time.minute}";
   }
}
 

Use it in your text like so:

Text(conversion!.getOnlyTime())

Upvotes: 3

Related Questions