0

I has setup flutter project get data from my Google sheet ,use Package gsheets 0.2.6 to get data when I get data use the print to check DateTime , I can verify is correct date time like this "2021-03-11 13:41:05" but when used the

factory ShpTable.fromGsheets(Map<dynamic, dynamic> json) {
    return ShpTable(
DateTime: DateTime.parse(json['DateTime'].toString()),
)}

I only can watch datevalue like this "44265.671747685185" I has tried used

DateTime.Parse(json['DateTime'].toString()).toString()

but get invalid data format

or

DateTime.tryParse(json['DateTime'].toString() ?? '').toString()

only feedback null

also I has tried

DateTime.fromMicrosecondsSinceEpoch(int.Parse(json['DateTime'].toString().trim() )

Still not work. How can i solve this issue?

my list

factory ShpTable.fromGsheets(Map<dynamic, dynamic> json) {
    return ShpTable(
      teamsName: json['TeamsName'].toString().trim(),
      teamOrde: json['TeamOrde'].toString().trim(),
      shpt: json['Shpt'].toString().trim(),
      dp: json['DP'].toString().trim(),
      delivery: json['Delivery'].toString().trim(),
      country: json['Country'].toString().trim(),
      items: int.tryParse(json['Items'] ?? ''),
      orde: json['Orde'].toString().trim(),
      shipTo: json['ShipTo'].toString().trim(),
      shipmentNo: json['ShipmentNo'].toString().trim(),
      shipmentDateTime: json['ShipmentDateTime'].toString().trim(),
      serviceStart: json['Service_Start'].toString().trim(),
      serviceEnd: json['Service_End'].toString().trim(),
      kpi: int.tryParse(json['KPI_Hour'] ?? ''),
      status: json['STATUS'].toString().trim() ,
      shpCondition: json['SHPCondition'].toString().trim(),
    );
  }
高小高
  • 37
  • 7
  • What is "44265.671747685185" rapresenting? If I convert "2021-03-11 13:41:05" to timestamp it would look somethin like like "1615466465000" so that value is not the unix timestamp – L. Gangemi Mar 11 '21 at 13:36
  • but used the Excel or Google Sheet you can convert Date String to Date Value, Like 2021-02-28 Datevalue("2021-02-28") ==> 44255 – 高小高 Mar 11 '21 at 13:40

2 Answers2

0

By using Datevalue on Google sheet the value you are getting is not a Unix timestamp.

I find out on Google sheet you can convert your string date to a Unix timestamp with this formula:

=INT((YOURCELL-("1/1/1970"-"1/1/1900"+2))*86400000)

Then you have to convert the unix timestamp you get to a DateTime on Flutter.

There it is:

String getDate(int UnixTimestamp){
    DateTime date = DateTime.fromMillisecondsSinceEpoch(UnixTimestamp);
    return date.toString();
  }
L. Gangemi
  • 3,110
  • 1
  • 22
  • 47
0

Thanks I summarized above answer to below code and does not need extra cell in google sheet . it is done only by flutter code :

d=DateTime.fromMillisecondsSinceEpoch(((double.parse(YOURCELLVALUE)-25569)*86400000).toInt(),isUtc: true);