0
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のタイムゾーンから日付データ部分を抜き出したい(時刻部分の削除)

Posted at

グーグルフォームを利用時に自動作成されるスプレッドシートにででてくるタイムゾーン。
GASで別シートへのデータの抜き出し処理をするときに、タイムゾーンの形式のままだと、時刻部分が邪魔なので、時刻部分を削除して日付データのみにする方法のメモです。

結論

■Utilities.formatDate(date, timeZone, format)

date→Date型 フォーマット対象の日付
timeZone → タイムゾーン
format → 日付のフォーマット ここでは時刻無しにフォーマットします。

■Utilities.formatDate公式サイトのURLは↓
https://developers.google.com/apps-script/reference/utilities/utilities#formatDate(Date,String,String)

■タイムゾーンは

Logger.log(Session.getTimeZone()); で出力できます。
もしくは
グーグルスプレッドシートのファイル→Googleスプレッドシートの設定タイムゾーンで確認できます。

image.png

■フォーマットはJavaのSimpleDateFormatと同じ

https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
今回は時間部分の削除なのでシンプルに yyyy/MM/ddでOKでした。

以上を考慮し
スプレッドシートのタイムゾーンから時間部分を削除します。

GoogleAppsScript

var hizuke="2020/07/30 5:44:25";   //セルからタイムゾーンを取得します。
var d2 = new Date(hizuke);           //日付をDate型にします。  
  
Logger.log(Utilities.formatDate(d2,"GMT+09:00","yyyy/MM/dd"));
Logger.log(Utilities.formatDate(d2,"Asia/Tokyo","yyyy/MM/dd"));


とても簡単なのですが、
一番調べたのがタイムゾーンの箇所
シンプルにそのまま文字列を記述すればよいのですが、なんかしっくりこなくて、多少時間がかかりました。

でも時間部分を削除することでスプレッドシートのフィルタがシンプルになったのでよかったです。

0
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
0
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?