首页 > 解决方案 > 将文本格式日期更改为日期格式

问题描述

嗨,我在多个列中有一组日期。

我需要将此单元格格式化为DD-MM-YYYY.

在谷歌表中更改为的日期"Mon Jun 01 10:06:35 2020"格式01-06-2020

请以我可以在任何谷歌表格中使用它的方式共享代码。

标签: javascript

解决方案


这是一个可能的解决方案:

function convertDate(x){
  const myDate = new Date(x);
  const day = myDate.getDate();
  const month = myDate.getMonth() + 1;
  const year = myDate.getFullYear()
  const formattedDate = `${day < 10 ? "0"+day : day}-${month < 10 ? "0"+month : month}-${year}`;
  return formattedDate;
}

console.log(convertDate("Mon Jun 01 10:06:35 2020"));
//Output: "01-06-2020"

推荐阅读