1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

GAS: `Utilities.formatDate` バグってる? 2020-12-28

Last updated at Posted at 2020-12-28

概要

Google Apps Script(GAS)の話。
今日(2020-12-28)から?
2020年なのに2021年になる

追記: ただのフォーマット指定ミスでした

内容

コード

function myFunction() {
  const date = new Date()
  console.log(date)
  console.log(Utilities.formatDate(date, 'JST', 'YYYY-MM-dd'))
  console.log(Utilities.formatDate(date, 'UTC', 'YYYY-MM-dd'))
  console.log(Utilities.formatString("%d-%02d-%02d", date.getFullYear(), date.getMonth() + 1, date.getDate()))
}

ログ

[20-12-28 10:26:16:935 JST] Mon Dec 28 2020 10:26:16 GMT+0900 (日本標準時)
[20-12-28 10:26:16:938 JST] 2021-12-28
[20-12-28 10:26:16:939 JST] 2021-12-28
[20-12-28 10:26:16:944 JST] 2020-12-28
  • date の段階では2020年
  • Utilities.formatDate の結果は2021年

まとめ

2020-12-28 10:28現在。
この記事のような状態。

取り急ぎ

Utilities.formatString("%d-%02d-%02d", date.getFullYear(), date.getMonth() + 1, date.getDate())

にして運用中

追記

formatの指定が悪かったみたい。
参考: Utilities.formatDate 年末の日付がおかしい??

コード:

function myFunction() {
  const date = new Date()
  console.log(date)
  console.log(Utilities.formatDate(date, 'JST', 'YYYY-MM-dd'))
  console.log(Utilities.formatDate(date, 'UTC', 'YYYY-MM-dd'))
  console.log(Utilities.formatString("%d-%02d-%02d", date.getFullYear(), date.getMonth() + 1, date.getDate()))
  
  console.log(Utilities.formatDate(date, 'UTC', 'yyyy-MM-dd'))
}

ポイント: 年は yyyy

ログ:

[20-12-27 17:36:46:446 PST] Mon Dec 28 2020 10:36:46 GMT+0900 (日本標準時)
[20-12-27 17:36:46:451 PST] 2021-12-28
[20-12-27 17:36:46:453 PST] 2021-12-28
[20-12-27 17:36:46:457 PST] 2020-12-28
[20-12-27 17:36:46:458 PST] 2020-12-28
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?