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】yyyy/MM/dd文字列から日時オブジェクトを取得する

Posted at

#はじめに
GoogleAppsScriptで「yyyy/MM/dd」表記の文字列から、日時オブジェクトを取得する方法です。
スプレッドシートから文字列で日時を取得した後、日時オブジェクトとして扱いたい時に利用しています。

#完成したコード

code.gs
var date = '2020/12/10';//日時を文字列で設定

var yy = date.slice(0,4);//→「年」の部分'2020'を切り出す
var mm = date.slice(5,7);//→「月」の部分'12'を切り出す
var dd = date.slice(8,10);//→「日」の部分'10'を切り出す

var dateObj = new Date(yy,mm-1,dd);//日時オブジェクト取得(月は0からカウントするので-1とする)
console.log(dateObj);

#結果

code.gs
Thu Dec 10 00:00:00 GMT+09:00 2020

以上です!

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?