Change the datetime format in flutter for this week's first day

Issue

I need to get the weeks first day as below format. the today comes good but not the first day. I can not assign the format for DateTime.now().day % 1
and import 'package:intl/intl.dart'; has assigned.

final dateFormatter = DateFormat('yMd');
DateTime now = DateTime.now();
String _today = DateFormat('yMd').format(now); //it is okay
var digit = DateTime.now().day % 1;
String digit2 = dateFormatter.digit; //I need this okay

Solution

This works but is however not the best approach for this. The structure can be modelled to suit you question. print used here is for testing alone.

 int subs, day = DateTime.now().weekday;

  getdate() {
    switch (day) {
      case 1:
        subs = 0;
        break;
      case 2:
        subs = 1;
        break;
      case 3:
        subs = 2;
        break;
      case 4:
        subs = 3;
        break;
      case 5:
        subs = 4;
        break;
      case 6:
        subs = 5;
      break;
      default:
      subs =6;
    }

        print(DateTime(DateTime.now().year,
                      DateTime.now().month, DateTime.now().day-subs));
  }

Answered By – king mort

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.