Convert date object from Google sheets to Firebase timestamp

前端 未结 1 694
礼貌的吻别
礼貌的吻别 2021-01-26 18:19

I\'ve got a date object in Google Sheet which I want to import to firestore as a timestamp object, but it doesn\'t work when I do it directly. I am using Google Apps Script.

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-26 19:17

    Suppose if your date object is like 3/2/2020 9:06:07 and from that, you need to extract only the date 2020-03-02 and for that date, if you want to get the timestamp 1583087400000 you can try the below solution.

           var dateSt = sourceData[i][0].toString();
           var stDate = new Date(dateSt);
           var stringfied = JSON.stringify(stDate);
           var updatedDt = stringfied.slice(1,11);
    
           //now u will have the date as 2020-03-02
    
           var myDate= updatedDt;
           myDate=myDate.split("-");
           var formattedDate=myDate[0]+"/"+myDate[1]+"/"+myDate[2];
    
           //Now the date is converted to 2020/03/02 (it should be in YYYY/MM/DD)
    
           var timestamp = new Date(formattedDate).getTime();
    
           //Now will have the timestamp for that particular date as 1583087400000
           data.date = timestamp;
    

    Suppose if u don't have a date in your google sheet and you want to create it from the app script means you can try this second solution

           var dateSt = new Date().toISOString().slice(0,10);
           //dateSt will have the date in 2020-06-04
           var myDate = dateSt;
           myDate = myDate.split("-");
           var formattedDate=myDate[0]+"/"+myDate[1]+"/"+myDate[2];
           //formatted date will be 2020/06/04
           var timestamp = new Date(formattedDate).getTime();
           //timeStamp will be 1591209000000
           data.date = timestamp;
    

    0 讨论(0)
提交回复
热议问题