dart 如何解决所选时间显示错误时间的问题?

7hiiyaii  于 2023-07-31  发布在  其他
关注(0)|答案(2)|浏览(125)

如何解决所选时间显示错误时间的问题?问题是所选时间将显示比我们所选时间早8小时的时间。例如,我选择8点。将显示的时间是下午4点。除了使用toUtc之外,还有其他方法可以解决这个问题吗?有人能帮帮我吗?
这是我目前为止的代码

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;
                    });
                  },
                ),
              ],
            ),
          );

字符串

mbzjlibv

mbzjlibv1#

尝试使用DateTime类的toLocal方法。

kgsdhlau

kgsdhlau2#

这个问题的原因可能是时区不匹配,使用toLocal方法来解决这个问题:

String timestampToDate(int timestamp, int type) {
  var date = DateTime.fromMillisecondsSinceEpoch(timestamp).toLocal();

  // Rest of your code remains unchanged...
}

字符串

相关问题