简体   繁体   English

从 Google Apps 脚本中的日期中删除时间戳

[英]Remove timestamp from date in Google Apps Script

I'm using this to snag a date value for cells that are created when a spreadsheet updates:我正在使用它来获取电子表格更新时创建的单元格的日期值:

setValue(new Date()).setNumberFormat('MM/dd/yyyy')

But what I get back includes the timestamp (ie 10/31/2015 22:16:55), rather than just the date (10/31/2015).但我得到的包括时间戳(即 10/31/2015 22:16:55),而不仅仅是日期(10/31/2015)。

This creates a problem with the logic I'm using that references that cell.这给我使用的引用该单元格的逻辑造成了问题。 The logic is a basic countif statements that references date only.该逻辑是仅引用日期的基本countif语句。 I could adjust to deal with timestamp but I would rather just not have the timestamp .我可以调整以处理timestamp但我宁愿没有timestamp Seems basic but I couldn't find any solution.看起来很基本,但我找不到任何解决方案。 What am I missing?我错过了什么?

The format won't change the fact that the date value is still containing the different times.格式不会改变日期值仍然包含不同时间的事实。

To truncate the time from the date value itself try the following:要从日期值本身截断时间,请尝试以下操作:

setValue(new Date(new Date().setHours(0,0,0,0))).setNumberFormat('MM/dd/yyyy');

You can try something like this:你可以尝试这样的事情:

var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd");

For more details Refer here有关更多详细信息,请参阅此处

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

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