最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove timestamp from date in Google Apps Script - Stack Overflow

programmeradmin2浏览0评论

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 (i.e. 10/31/2015 22:16:55), rather than just the date (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. I could adjust to deal with timestamp but I would rather just not have the timestamp. Seems basic but I couldn't find any solution. What am I missing?

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 (i.e. 10/31/2015 22:16:55), rather than just the date (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. I could adjust to deal with timestamp but I would rather just not have the timestamp. Seems basic but I couldn't find any solution. What am I missing?

Share Improve this question edited Jul 21, 2020 at 17:05 Wicket 38.1k9 gold badges77 silver badges189 bronze badges asked Nov 1, 2015 at 5:17 DaveDave 1011 gold badge1 silver badge3 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 14

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

example implementation would be something like:

// where sheet is a predefined sheet, and getRange is cell F1
sheet.getRange(1,6).setValue(formattedDate).setNumberFormat('MM/dd/yyyy')

For more details Refer here

发布评论

评论列表(0)

  1. 暂无评论