简体   繁体   English

从 Google 表格重新格式化日期

[英]Reformatting date from Google Sheets form

I'm using Google Forms to submit data into a Google Sheet.我正在使用 Google Forms 将数据提交到 Google 表格中。 When I log the cell value which contains the date, it returns "Thu Apr 22 03:00:00 GMT-04:00 2021"当我记录包含日期的单元格值时,它返回“Thu Apr 22 03:00:00 GMT-04:00 2021”

I want the date to look like "April 22, 2021"我希望日期看起来像“2021 年 4 月 22 日”

Any advice on how to accomplish this?关于如何做到这一点的任何建议? Any hints would be appreciated.任何提示将不胜感激。

Thank you.谢谢你。

Follow @MetaMan's advice and use Utilities.formatDate() .遵循@MetaMan 的建议并使用Utilities.formatDate() The timezone should be set to that of the dates you receive from the spreadsheet, because the script may be in a different timezone:时区应设置为您从电子表格收到的日期,因为脚本可能位于不同的时区:

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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM