首页 > 解决方案 > Apps 脚本:如何将英文日期格式转换为德文日期格式?

问题描述

我很确定这是一个小问题,但我只是不知道如何解决它,因为我是使用 Apps 脚本的新手 :(。

问题:

我有一张由 2 列组成的表格:A 和 B。A 列包含以德语格式(日/月/年)编写的日期。示例:2021 年 10 月 18 日写成 18/10/2021。现在我要做的就是每天从 A 列增加 1 天,然后在 B 列中写入结果(因此,增加的天数)。

尝试的解决方案:

我已经想出了如何增加天数并将结果写在 B 列中。但我正在努力添加脚本的第二部分,以便在 B 列中用德语写出增加的日期(参见下面的脚本)。

问题:

如何以及在何处将“Utilities.formatDate()”包含到我的代码中,以便 B 列中的日期以德语格式化?(所以,DAY/MONTH/YEAR而不是 MONTH/DAY/YEAR)?

非常感谢您的帮助。

// Increment dates in colum A by 1 day and write this incremented date in colum B
function incrementDateInGermanFormat() {
  var ss = SpreadsheetApp.getActiveSheet();
  var date = new Date(ss.getRange(1, 1).getValue());
  ss.getRange(1, 2).setValue(new Date(date.setDate(date.getDate() + 1)));
}

// This is the line I´m having trouble to include in the script above
Utilities.formatDate(date, Session.getScriptTimeZone(), "dd/MM/yyyy")

标签: dategoogle-apps-scriptgoogle-sheets

解决方案


将日期转换为字符串是不好的做法。在这种情况下,更好的选择是在电子表格用户界面中将 B 列的日期格式简单地更改为德语或.setNumberFormat("dd/mm/yyyy")在 B 列上使用。


推荐阅读