0

I have wrote following script in my google sheet hoping to convert a timestamp in GMT to a customized timezone.

function myFunctions(datetimeString,timeZone,format) {
  var moment = new Date(datetimeString);
  return Utilities.formatDate(moment, timeZone, "yyyy-MM-dd'T'HH:mm:ss'Z'")
}

However, I have no idea why this function doesn't give me a correct timezone ! For instant, 6/30/2019 6:32:00 should return June 30, 2019 11:02 AM in (GMT+4:30), However, it returns wrong time (June 30, 2019 02:02).

I've wrote the script in script tools, and call myFunctions in googlesheet's cell (consider my timestamp is in A1):

=myFunctions(A1,Aisa/Tehran,"dd-MM-yyyy'T'HH:mm:ss'Z'")

Thanks in advance.

1
  • Please add the timezone to the input date. If the input date is cell value, the timezone is the spreadsheet timezone. Commented Jul 1, 2019 at 16:22

1 Answer 1

1

To pass strings as arguments to a custom functions they should be quoted enclosed.

Instead of

=myFunctions(A1,Aisa/Tehran,"dd-MM-yyyy'T'HH:mm:ss'Z'")

use

=myFunctions(A1,"Asia/Tehran","dd-MM-yyyy'T'HH:mm:ss'Z'")
1
  • 1
    Thanks for your reply. However, it doesn't work ! In fact I get a time conversion, but it's not correct ! It shows another time which is not the one I expect ! I'm not sure why it gives me a wrong timezone !!! Is it a bug ?
    – Majid
    Commented Jul 1, 2019 at 6:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.