Home > other >  Custom Date and Time Format
Custom Date and Time Format

Time:05-23

I have successfully used the setNumberFormat() function for a range that contained numbers. Is there a similar function for custom date and time formats?

// formatting
  eSheet.getRange(earRng).setNumberFormat('_(\"$\"* #,##0.00_);_(\"$\"* \\(#,##0.00\\);_(\"$\"* \"-\"??_);_(@_)');
  eSheet.getRange(datesRange).setNumberFormat('Aug-1930');

Hopefully seeing the code will give you a good idea of what I am trying to achieve.

Appreciate any help I can get with this!

Sample input (column A):

earnings before format

Expected output (column A):

earnings after format

I can achieve this by going to "Format" -> "Number" -> "Custom date and time", but would like to know how to do the same via code if possible.

CodePudding user response:

You can use Range.setNumberFormat() with date objects as well, like this:

  eSheet.getRange(datesRange).setNumberFormat('MMM-yyyy');

See Date and Number Formats.

  • Related