【问题标题】:Adding a Time to a Date stamp before posting to Firebase在发布到 Firebase 之前向日期戳添加时间
【发布时间】:2021-03-31 11:48:10
【问题描述】:

现在我有一个当前问题,因为英国已进入夏令时(即从 UTC 移至 UTC+1)。

存储为例如 3 月 31 日 00:00:00 UTC+1 的日期被读取为 3 月 30 日 23:00:00 UTC。应用程序在向用户显示时正确读取日期,但我有一个通知系统在错误的日期发送通知。

日期来自表单生成器字段。我不希望向用户显示时间,只是日期,但我想要做的是在凌晨 2 点将此日期发布到 Firestore 集合。例如,如果用户输入 2021 年 3 月 31 日 - 我希望将 2021 年 3 月 31 日 02:00:00 添加到集合中。

我怎样才能做到最好?

我的表单生成器代码是:

FormBuilderDateTimePicker(
                                attribute: 'dateChosen',
                                onChanged: _onChanged,
                                inputType: InputType.date,
                                format: DateFormat("dd-MM-yyyy"),
                                decoration: const InputDecoration(
                                  labelText: 'Add Date',
                                ),
                                validator: (val) => null,
                                initialValue: DateTime.now(),
                                validators: [
                                  FormBuilderValidators.required(),
                                  (val) {
                                    if (val.compareTo(DateTime.now()) < 0)
                                      return "The end date must not be in the past";
                                  },
                                ],
                              ),

将其发布到 Collection 的代码如下:

_firestore
                                            .collection(
                                                'thisCollection')
                                            .add({
                                          'Type': 'Niche',
                                          'nicheName': _fbKey
                                              .currentState.value['nicheName'],
                                          'Date': _fbKey
                                              .currentState.value['dateChosen'],
                                          'Posted': DateTime.now(),
                                          'uid': getCurrentUID(),
                                        });

【问题讨论】:

    标签: forms flutter datetime google-cloud-firestore


    【解决方案1】:

    您可以使用FieldValue.serverTimestamp()

    例如:

    'Posted': firebase.firestore.FieldValue.serverTimestamp(),
    

    【讨论】:

      猜你喜欢
      • 2011-09-02
      • 2016-02-26
      • 2021-12-03
      • 2023-03-31
      • 2017-09-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多