G
G
Grigory Boev2020-05-13 15:05:38
Google Apps Script
Grigory Boev, 2020-05-13 15:05:38

How to set Russian locale in Utilites.formatDate()?

Good afternoon. The task is to get the "May-2020" format string in Google Apps Script via Utilities.formatDate() . I do it like this:

var newSheetname = Utilities.formatDate(new Date(), "GMT+3", "MMM-d"); // May-2020

The question is how to change the locale from 'en' to 'ru'. The documentation says that it uses a string format similar to Java's SimpleDateFormat , but Java has a constructor that takes a locale, and here it's just a date and 2 strings - a time zone and a format.
There is definitely a solution, I saw it somewhere in the code at one time, but did not rewrite it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ivanov, 2020-05-14
@ProgrammerForever

I don't remember Utilitieslocales being supported.
For V8 you can useDateTimeFormat

function myFormatDate(date) {
  const y = new Intl.DateTimeFormat('ru',{
    year: 'numeric'
  }).format(date)
  const m = new Intl.DateTimeFormat('ru',{
    month: 'long'
  }).format(date)
  return `${m}-${y}`
}

function test(){
  const date = new Date();
  console.log(Utilities.formatDate(date, "GMT+3", "MMM-yyyy"));
  console.log(myFormatDate(date));
}

In theory, there is no need to worry about performance in such places, in general they are well optimized by the system.
Pay attention to your example, "MMM-d"it will not return the year - it will return the date.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question