1

I recently started with Flutter and I love how it works. I need to create an App for iOS and Android for the company I work for, so I started with Flutter.

Our website uses Wordpress so I am trying to use that as the backend of the App.

It is going Ok and in the App I have a News section, so I have written some code and did a Wordpress API call and it works great!

I only have one Issue: the date and time format is wrong. We are in Europe so we use dd-mm-yyyy format and in my App the format is like this: yyyy-mm-dd and I need to change that but I don't know how...

My Code:

class Post {
  final int id;
  final String title;
  final String author;
  final String excerpt;
  final String date;
  final String content;
  final String image;
  bool isSaved = false;

  Post(
    {
      this.content,
      this.id,
      this.title,
      this.excerpt,
      this.date,
      this.image,
      this.author,
    }
  );

  factory Post.fromJSON(Map<String, dynamic> json) {
    return Post(
      id: json['id'],
      title: json['title']['rendered'],
      content: json['content']['rendered'],
      date: json['date'] != null
        ? json['date'].toString().replaceFirst('T', ' ')
        : null,
      image: json['_links']['wp:featuredmedia'] != null
        ? json['_links']['wp:featuredmedia'][0]['href']
        : null,
      excerpt: json['excerpt']['rendered'],
      author: json['author'].toString()
    );
  }
}

Can someone please take a look and help me out?

Thanks!

deczaloth
  • 7,094
  • 4
  • 24
  • 59
EnricovanDijkhuizen
  • 153
  • 1
  • 3
  • 10
  • Does this answer your question? [How do I format a date with Dart?](https://stackoverflow.com/questions/16126579/how-do-i-format-a-date-with-dart) – Tirth Patel May 26 '21 at 19:13

2 Answers2

2

Try this

  var data = DateFormat('dd-MM-yyyy').format(DateTime.parse('2020-04-12'));
  print(data);//12-04-2020
Ajay Kumar
  • 15,250
  • 14
  • 54
  • 53
  • Also @Enrico I would store the date using a DateTime inside the post and have a method or getter to format it. That way you won't lose the ability to compare dates accurately. – croxx5f May 26 '21 at 19:23
1

Fix without using external packages

Instead of

date: json['date'] != null
  ? json['date'].toString().replaceFirst('T', ' ')
  : null,

you could write something like

date: json['date'] != null
  ? getFormattedDate(json['date'].toString())
  : null,

where

String getFormattedDate(String dtStr) {
  var dt = DateTime.parse(dtStr);
  
  return "${dt.day.toString().padLeft(2,'0')}-${dt.month.toString().padLeft(2,'0')}-${dt.year} ${dt.hour.toString().padLeft(2,'0')}:${dt.minute.toString().padLeft(2,'0')}:${dt.second.toString().padLeft(2,'0')}.${dt.millisecond .toString().padLeft(3,'0')}";
}

Fix using intl package

Instead of

date: json['date'] != null
  ? json['date'].toString().replaceFirst('T', ' ')
  : null,

write something like

date: json['date'] != null
  ? DateFormat('dd-MM-yyyy HH:mm:ss').format(DateTime.parse(json['date'].toString())),
  : null,
deczaloth
  • 7,094
  • 4
  • 24
  • 59