skip to Main Content

How to solve the issue of the selected time showing the wrong time?
The problem is that the selected time will show a time 8 hours earlier than what we selected. For example, I choose 8 am. The time that will be shown is 4 pm. Is there any other way to solve this problem other than using toUtc? Can anyone help me?

Here’s my code so far

 dynamic _appointmentDateEpocMilisecond = "";
    String _selectedTime = "08:00:00.000z";
    
    String timestampToDate(int timestamp, int type) {
      var date = DateTime.fromMillisecondsSinceEpoch(timestamp);
      //var formatter = new DateFormat('dd/MM/yyyy hh:mm a');
      var formattedDate = DateFormat('dd/MM/yyyy').format(date).toString();
      if (type == 1) {
        formattedDate = DateFormat('hh:mm a').format(date).toString();
      } else if (type == 2) {
        formattedDate = DateFormat('dd/MM/yyyy hh:mm a').format(date).toString();
      } else if (type == 3) {
        formattedDate = DateFormat('EEE, d MMM, yyyy').format(date).toString();
      } else if (type == 4) {
        formattedDate =
            DateFormat('EEE, d MMM, yyyy hh:mm a').format(date).toString();
      } else if (type == 5) {
        formattedDate = DateFormat('d MMM').format(date).toString();
      }
      return formattedDate;
    }
    
    
    onPressed: () async {
    switch (_step) {
    case 3:
           if (_selectedDay.toString().length > 1) {
           var formattedDate = DateFormat('yyyy-MM-dd')
           .format(_selectedDay!)
           .toString();
            var createDateTime =
            "$formattedDate $_selectedTime";
             var appointmentDateEpocMilisecond =
                      DateTime.parse(createDateTime)
                      .millisecondsSinceEpoch;
                        _appointmentDateEpocMilisecond =
                            appointmentDateEpocMilisecond;
    
                       }
                         }
                          }

    Container(
                        padding: const EdgeInsets.fromLTRB(20, 0, 20, 20),
                        color: AppConst.bgLightBlue,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            const SizedBox(height: 30),
                            const Text(
                              "Choose available time",
                              style: TextStyle(
                                  fontSize: 20,
                                  fontWeight: FontWeight.w600,
                                  color: Color(0xFF172B37)),
                            ),
                            const SizedBox(height: 10),
                            customTabHeader(),
                            _tabIndex == 0
                                ? Column(
                                    children: [
                                      customRadio("8:00 AM", "08:00:00.000Z"),
                                      customRadio("9:00 AM", "09:00:00.000Z"),
                                      customRadio("10:00 AM", "10:00:00.000Z"),
                                      customRadio("11:00 AM", "11:00:00.000Z"),
                                    ],
                                  )
                                : Column(
                                    children: [
                                      customRadio("12:00 PM", "12:00:00.000Z"),
                                      customRadio("1:00 PM", "13:00:00.000Z"),
                                      customRadio("2:00 PM", "14:00:00.000Z"),
                                      customRadio("3:00 PM", "15:00:00.000Z"),
                                      customRadio("4:00 PM", "16:00:00.000Z"),
                                      customRadio("5:00 PM", "17:00:00.000Z"),
                                      customRadio("6:00 PM", "18:00:00.000Z"),
                                      customRadio("7:00 PM", "19:00:00.000Z"),
                                      customRadio("8:00 PM", "20:00:00.000Z"),
                                      customRadio("9:00 PM", "21:00:00.000Z"),
                                    ],
                                  ),
                          ],
                        ),
                      )

 

    Expanded(
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.start,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              const Text("Time", style: TextStyle(fontSize: 12)),
                              Text(timestampToDate(
                                  _appointmentDateEpocMilisecond, 1)),
                              //Text(_timestampToDate(element.appointmentUnixTime,1)
                            ],
                          ),
                        )

    Widget customRadio(String time, String time24) => Container(
            margin: const EdgeInsets.only(bottom: 10),
            padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 5),
            decoration: const BoxDecoration(
                borderRadius: BorderRadius.all(
                  Radius.circular(5.0),
                ),
                boxShadow: <BoxShadow>[
                  BoxShadow(
                      color: Color(0xFFE1EAF0),
                      blurRadius: 10.0,
                      offset: Offset(0.0, 1.0))
                ],
                color: Colors.white),
            child: Row(
              children: [
                Expanded(
                  child: Text(
                    time,
                    style: const TextStyle(fontSize: 20, color: Color(0xFF2C3F4C)),
                  ),
                ),
                Radio(
                  value: time24,
                  groupValue: _selectedTime,
                  onChanged: (value) {
                    setState(() {
                      _selectedTime = value.toString();
                      _nextMediumButtonAction = true;
                    });
                  },
                ),
              ],
            ),
          );

2

Answers


  1. Try using toLocal method of DateTime class.

    Login or Signup to reply.
  2. Probably the reason of this problem is mismatch in time zones, use toLocal method to solve the issue:

    String timestampToDate(int timestamp, int type) {
      var date = DateTime.fromMillisecondsSinceEpoch(timestamp).toLocal();
    
      // Rest of your code remains unchanged...
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search