Home > Software design >  How do I capitalize the first letter of day in DateFormat in French?
How do I capitalize the first letter of day in DateFormat in French?

Time:11-11

I am using FRENCH Language in my mobile.

DateFormat('MMMMEEEEd').format(DateTime.now()) 

It prints vendredi 11 novembre

Expected output => Vendredi 11 novembre

Is there any way to capitalize first letter without using string methods?

I don't want to capitalize the first letter of string with toUpperCase() method

CodePudding user response:

The french weekday is lowercase in standard. So there is no function to make it uppercase. It will break the standardisation rules. You need to handle it by your self.

Capitalize in French

import 'package:intl/intl.dart';
import 'package:intl/date_symbol_data_local.dart';

void main(){
  getDate(String lang){
    switch (lang){
      case 'fr':
        String date;
        initializeDateFormatting('fr_FR', null).then((_) => {
          date = DateFormat.MMMMEEEEd(lang).format(DateTime.now()),
          date = date.replaceRange(
            0,
            1, 
            date.substring(0,1).toUpperCase(),
          ),
          print(date)
        });
        break;
      case 'en':
        initializeDateFormatting('en_US', null).then((_) => {
          print(DateFormat.MMMMEEEEd(lang).format(DateTime.now()))
        });
        break;
    }
  }
  
  getDate('fr');
  getDate('en');
}

Output:

Vendredi 11 novembre
Friday, November 11

CodePudding user response:

You can create subString

final data = "vendredi 11 novembre";

if (data.length > 1) { //just making sure string length
  final result = data[0].toUpperCase()   data.substring(1);

  print(result); //Vendredi 11 novembre
}

You can also create extension or method. More about String

  • Related