2

In google sheets when I insert a date it saves it in the right format when I get that value I get an int number I insert this 1/6/2021 & I get that 44202 1/6/2021 => 44202 how to convert this number to date in dart or java or any language

I get it from google sheets API using a package so it gives me a number

https://pub.dev/packages/gsheets

Abdelrahman Tareq
  • 1,874
  • 3
  • 17
  • 32

5 Answers5

6

44202 refers to the number of days passed since the epoch time used in spreadsheets, which refers to December 30th, 1899.

If you want to retrieve the corresponding date in Dart, you can just add this number of days to this epoch date. For example, you could do this:

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

Reference:

Iamblichus
  • 18,540
  • 2
  • 11
  • 27
0

internaly:

enter image description here


externaly:

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

enter image description here

player0
  • 124,011
  • 12
  • 67
  • 124
0

Iamblichus's answer is the right answer but I'd like to add some details about how to convert hours, minutes and seconds, that I found on 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));
Samuel T.
  • 194
  • 3
  • 13
0

Go ahead Use this to convert both time and date Dart Version!

Formated Date Will be like this 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;
  }

Formated Time Will be like this 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;
  }
0

If you are using a ListView to display items then you could do it like this:

******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))))),
Mario360
  • 1
  • 3