1
1

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 5 years have passed since last update.

JavaScriptで文字列要素を切り出す

Last updated at Posted at 2016-07-19

やりたいこと

(例) 2016-04-21 みたいなデータが存在する。
(2016)みたいにしたいので、先頭の4文字だけ切り出したい。

処理


function sliceDate(aDate) {
  let date = aDate;
  let sliceData = date.splice(0, 4); // 先頭から4文字まで取得する
  return sliceData; // 値を返す
}

let rel = sliceDate('2016-03-21'); // 元の日付データ
console.info(`(${rel})`); // (2016)

追記

sliceの中の第一引数、第二引数を変えることで、要素を切り出したい場所を変更できる。

1
1
2

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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?