Home > Back-end >  i want to remove timestamp using flutter
i want to remove timestamp using flutter

Time:10-18

The end date comes like 2022-11-02 00:00:00.000 how to remove this timestamp

 onPressed: () async {
                      String startDate = startDateController.text;

                      String duration = durationController.text;

                      final int dur = int.parse(durationController.text);

                      var stDate = DateTime.parse(startDateController.text);

                      final DateFormat formatter = DateFormat('yyyy-MM-dd');
                      final String formatted = formatter.format(stDate);
                      print(formatted);

                      DateTime thirtyDaysFromNow =
                          stDate.add(new Duration(days: dur));

                      setState(() {
                        startDate;
                        duration;
                        // endDateController.text =
                        //     formatted.thirtyDaysFromNow.toString();

                        endDateController.text = thirtyDaysFromNow.toString();
                      });

                      //print(thirtyDaysFromNow);
                    },
                    child: const Text("Save"),
                    style: ButtonStyle(
                        backgroundColor: MaterialStateProperty.all(
                            Color.fromARGB(255, 3, 89, 168))),
                  ))

CodePudding user response:

The stDate formatter is working fine. You can format the end data before adding on controller,

Your formatter

final DateFormat formatter = DateFormat('yyyy-MM-dd');

And while adding text to the controller

DateTime thirtyDaysFromNow = DateTime.parse(startDate);
endDateController.text = formatter.format(thirtyDaysFromNow);

CodePudding user response:

I simulated it here for you, try it:

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class DateCustom extends StatefulWidget {
  const DateCustom({Key? key}) : super(key: key);

  @override
  State<DateCustom> createState() => _DateCustomState();
}

class _DateCustomState extends State<DateCustom> {
  TextEditingController startDateController = TextEditingController();
  TextEditingController durationController = TextEditingController();
  TextEditingController endDateController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('DateCustom'),
      ),
      body: Column(
        children: [
          TextFormField(
            controller: startDateController,
          ),
          TextFormField(
            controller: durationController,
          ),
          TextFormField(
            controller: endDateController,
          ),
          ElevatedButton(
              onPressed: () {
                String startDate = startDateController.text;

                String duration = durationController.text;

                final int dur = int.parse(durationController.text);

                var stDate = DateTime.parse(startDateController.text);

                final DateFormat formatter = DateFormat('yyyy-MM-dd');
                final String formatted = formatter.format(stDate);

                //avoid print();
                debugPrint(formatted);

                DateTime thirtyDaysFromNow = stDate.add(Duration(days: dur));

                setState(() {
                  startDate;
                  duration;

                  endDateController.text = formatter
                      .format(DateTime.parse(thirtyDaysFromNow.toString()));
                });
              },
              child: const Text('Date')),
        ],
      ),
    );
  }
}

input: 2022-10-18 5 output: Must be -> 2022-10-23

  • Related