Dartime Format (month name with minuscule letter)

Asked

Viewed 52 times

0

I am formatting the following date "2020-03-25" and I want it to come out "March 25" but with my formatting it is coming out "March 25" the name of the month is coming out with minuscule letter, it is possible to tidy up?

String dateFormate = DateFormat("dd' 'MMMM", 'pt_BR').format(2020-03-25);

1 answer

1

After looking at the Documentation of the Dateformat class, no other parameter seems to do what you want. This "uppercase" information seems to come directly from when you select the locale pt_BR.

However, you can do a function for such:

String tornarMesMaiusculo(String data){
    List<String> palavras = data.split(" ");
    palavras.last = "${palavras.last[0].toUpperCase()}${palavras.last.substring(1)}";
    return (palavras.join(" "));
  }

If you prefer, you can make a extension to be able to call directly:

extension mesMaiusculo on String{

  String tornarMesMaiusculo(){
    List<String> palavras = this.split(" ");
    palavras.last = "${palavras.last[0].toUpperCase()}${palavras.last.substring(1)}";
    return (palavras.join(" "));
  }
}

So you can call directly:

String dateFormate = '25 de março';
print (dateFormate.tornarMesMaiusculo()); // imprime "25 de Março"

Do not forget that a function of this type assumes the String to be this specific way, and you should take precautions not to call it with empty strings for example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.