【问题标题】:Reformatting date from Google Sheets form从 Google 表格重新格式化日期
【发布时间】:2021-04-23 01:27:51
【问题描述】:

我正在使用 Google 表单将数据提交到 Google 表格中。当我记录包含日期的单元格值时,它返回“Thu Apr 22 03:00:00 GMT-04:00 2021”

我希望日期看起来像“2021 年 4 月 22 日”

关于如何做到这一点的任何建议?任何提示将不胜感激。

谢谢。

【问题讨论】:

  • Utilties.formatDate(new Date('Thu Apr 22 03:00:00 GMT-04:00 2021'),Session.getScriptTimezone(), "MMM dd, yyyy"); 或者您可以使用电子表格格式化工具格式化单元格。
  • @MetaMan 谢谢!

标签: javascript google-apps-script


【解决方案1】:

听从@MetaMan 的建议并使用Utilities.formatDate()。时区应设置为您从电子表格收到的日期,因为 脚本 可能位于不同的时区:

function logFormResponseDates() {
  const ss = SpreadsheetApp.getActive();
  const dates = ss.getSheetByName('Form Responses 1')
    .getRange('A2:A').getValues().flat().filter(String);
  const timezone = ss.getSpreadsheetTimeZone();
  const formattedDates = dates.map(date => Utilities.formatDate(date, timezone, 'mmmm d, yyyy'));
  console.log(JSON.stringify(formattedDates));
}

【讨论】:

    猜你喜欢
    • 2012-06-22
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多