flutter Google表单获取日期值作为数字问题

xghobddn  于 2023-01-14  发布在  Flutter
关注(0)|答案(5)|浏览(107)

在谷歌工作表中,当我插入一个日期时,它会以正确的格式保存它,当我得到那个值时,我得到一个int数字,我插入这个1/6/2021 &我得到那个44202 1/6/2021 =〉44202如何在dart或java或任何语言中将这个数字转换为日期
我使用一个包从Google Sheets API获得它,因此它会给我一个数字
https://pub.dev/packages/gsheets

ryoqjall

ryoqjall1#

44202指的是自电子表格中使用的纪元时间(指December 30th, 1899)以来经过的天数。
如果要在Dart中检索相应的日期,只需将此天数添加到此纪元日期。例如,可以执行以下操作:

var epoch = new DateTime(1899,12,30);
var currentDate = epoch.add(new Duration(days: 44202));
print(currentDate); // 2021-01-06

参考:

wlsrxk51

wlsrxk512#

内部:

外部:

=TEXT(A1; "dd/mm/yyyy")

2ic8powd

2ic8powd3#

Iamblichus的答案是正确的,但我想补充一些关于如何转换小时,分钟和秒的细节,我在this Javascript answer上找到:

var fractionalDay = serial - serial.floor() + 0.0000001;
var totalSeconds = (86400 * fractionalDay).floor();
var seconds = totalSeconds % 60;
totalSeconds -= seconds;
var hours = (totalSeconds / (60 * 60)).floor();
var minutes = (totalSeconds / 60).floor() % 60;

var epoch = DateTime(1899,12,30);
var currentDate = epoch.add(Duration(days: serial.floor(), hours: hours, minutes: minutes, seconds: seconds));
nhjlsmyf

nhjlsmyf4#

继续使用此功能转换时间和日期Dart版本
格式化日期如下所示5-12-1992

static String? formatSheetDate(String? dateGot) {
    if (dateGot == null || dateGot == '') return null;
    final dateSome = DateTime.fromMillisecondsSinceEpoch(
        ((double.parse(dateGot) - 25569) * 86400000).toInt(),
        isUtc: true);
    var dateTime = DateTime.parse(dateSome.toString());

    var formatDate = "${dateTime.day}-${dateTime.month}-${dateTime.year}";

    return formatDate;
  }

格式化时间如下所示08:20 AM

static String? formatSheetTime(String? timeGot) {
    if (timeGot == null || timeGot == '') return null;
    final dateSome = DateTime.fromMillisecondsSinceEpoch(
        ((double.parse(timeGot) - 25569) * 86400000).toInt(),
        isUtc: true);
    var dateTime = DateTime.parse(dateSome.toString());

    var formattedTime = DateFormat('hh:mm a').format(dateTime);

    return formattedTime;
  }
ajsxfq5m

ajsxfq5m5#

如果您使用ListView来显示项目,那么您可以这样做:

******Code in Multiple Lines******
body: ListView.builder(
    itemCount: _transazione.length,
    itemBuilder: (context, index) {
        final transazione = _transazione[index];
        final epoch = DateTime(1899, 12, 30);
        final currentDate = epoch.add(Duration(days: (int.tryParse(transazione.data) ?? 0)));
        final formatter = DateFormat("dd/MM/yyyy").format(currentDate);
        return ListTile(
            title: //...
            subtitle: Text(formatter),
            trailing: //...
        );
    },
),

******Code in One Line:******
subtitle: Text(DateFormat("dd/MM/yyyy").format(DateTime(1899, 12, 30).add(Duration(days: (int.tryParse(transazione.data) ?? 0))))),

相关问题