根据电子表格区域设置获取时间格式? [英] Get time format according to spreadsheet locale?

查看:92
本文介绍了根据电子表格区域设置获取时间格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据电子表格的区域设置( SpreadsheetApp.getActive().getSpreadsheetLocale())以正确的格式将Javascript Date()对象存储在电子表格中.br>有没有办法从电子表格区域设置中获取国家/地区特定的(日期和)时间格式字符串?
例如.当语言环境为de_DE时,时间格式字符串为 hh:mm
但是当语言环境为da_DK时,时间格式字符串为 hh.mm
有趣的是如何获取国家/地区的货币格式.

顺便说一句,当我在 de_DE 中有日期和时间,然后更改为 da_DK 时,日期将重新格式化为(23.01.2020-> 23/01/2020 >),但时间不是(时间保持为 22:59 ).这是电子表格中的错误吗?

I want to store a Javascript Date() object in a spreadsheet with correct format according to spreadsheet's locale (SpreadsheetApp.getActive().getSpreadsheetLocale()).
Is there a way to get the country specific (date and) time format string from the spreadsheet locale?
E.g. when locale is de_DE, time format string as hh:mm
but when locale is da_DK, time format string as hh.mm
Interesting as well how to get the countries currency format.

BTW when I have date and time in de_DE and than change to da_DK, dates are reformatted (23.01.2020 -> 23/01/2020) but times are not (it stays as 22:59). Is that an error in Spreadsheet?

推荐答案

JavaScript中的日期具有方法

Dates in JavaScript have the method toLocaleDateString, which return a string formatted according to the specified locale. But this doesn't seem to work in Apps Script.

如果您愿意使用 Apps Script Web App 为此,您可以在客户端脚本(即HTML的脚本标签)中使用此 toLocaleDateString .

If you're open to using an Apps Script Web App for this, you could use this toLocaleDateString in your client-side script (that is, in a script tag in your HTML).

如果不是这种情况,我认为您最好的选择是自己创建格式和语言环境之间的关系,因为Apps Script没有内置的方法来实现这一点.例如,您可以使用开关会检查语言环境的语句,然后使用

If that's not the case, I think your best option would be to create the relationship between formats and locales yourself, because Apps Script doesn't have a built-in method to achieve that. You could, for example, use a switch statement that would check the locale, and then format the date accordingly with Utilities.formatDate, the tool Apps Script uses to format dates. It could be something along the following lines:

var locale = SpreadsheetApp.getActive().getSpreadsheetLocale();
var formattedDate;
switch (locale) {
  case 'de_DE':
    formattedDate = Utilities.formatDate(yourDate, yourTimeZone, "hh:mm");
    break;
  case 'da_DK':
    formattedDate = Utilities.formatDate(yourDate, yourTimeZone, "hh.mm");
    break;
    // ...
}
return formattedDate;

参考:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆