3

I am currently using the following code in Google scripts, but it only displays the date.
How can I add the hh:mm:ss to the value when I type "_now"?

function onEdit(e) {
  if (e.range.getValue() == "_now") {
    e.range.setValue(new Date());
  }
}
4

The script actually does set both date and time. The reason you don't see it is that the cell is formatted to show only the date.

You can either change its format manually, or do it within the script:

function onEdit(e) {
  if (e.range.getValue() == "_now") {
    e.range.setValue(new Date()).setNumberFormat("yyyy-MM-dd HH:mm:ss");
  }
}

if you want the ISO standard 2016-12-25, or

function onEdit(e) {
  if (e.range.getValue() == "_now") {
    e.range.setValue(new Date()).setNumberFormat("MM/dd/yyyy HH:mm:ss");
  }
}

if you prefer 12/25/2016 instead.

  • Perfect, thank you. And fun fact because of your user name, this is a project for a winery. – Trevor Aug 3 '16 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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