简体   繁体   中英

Convert date format without timezone - Google app script (Javascript), Google spreadsheet

I'm working on integration between Google spreadsheet and LINE Message API (BOT) where Google app script is back-end.

I get the date-format cell from Google spreadsheet and send to LINE bot but the message reply showed different thing. in a cell of Google Spreadsheet

1/5/2020

In Google app script, I first coded it

var colb =  ss.getSheets()[0].getRange(i+3, 2).getValue();

but LINE message, it sends format included timezone as default

Sun Jan 05 2020 00:00:00 GMT+0700 (ICT)

So I coded it

var colb =  Utilities.formatDate(ss.getSheets()[0].getRange(i+3, 2).getValue(), ss.getSpreadsheetTimeZone(), "dd/MM/YY");

This one is works but it's not working when cell is empty which is I have no clue why. So someone please help me with this. Thank in advance

If cell is empty and you want to use Utilities.formatDate I would do like this:

var colb =  " " || Utilities.formatDate(ss.getSheets()[0].getRange(i+3, 2).getValue(), ss.getSpreadsheetTimeZone(), "dd/MM/YY");

So in case there is no value in cell just assign default value " "

If you need colb to contain the original value (including empty) when the date conversion doesn't work, you can do this:

var value = ss.getSheets()[0].getRange(i+3, 2).getValue();
var colb = (typeof value == "object" && value instanceof Date) ? Utilities.formatDate(value, ss.getSpreadsheetTimeZone(), "dd/MM/YY") : value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM