LoginSignup
0
0

More than 1 year has passed since last update.

【javascript】stringのyyyy年mm月dd日をyyyymmddにフォーマットする。

Last updated at Posted at 2021-09-06

業務の中でどうしても年月日表記のデータを数字だけの表記に変換したい。

例)
渡ってくるデータ → 2021年09月06日(月)
変換後の値    → 20210906

moment.jsを使用して変更

index.html
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.js"></script>
</head>
<div id="date">2021年09月06日(月)</div>
<script>
var get_id = document.getElementById("date")
var get_value = get_id.textContent
console.log(moment(get_value, 'YYYYMMDD').format('YYYYMMDD')) //20210906
</script>



replace()で変更


コメントにて @gsw213 様より
index.html
<div id="date">2021年09月06日(月)</div>
<script>
  const elem = document.getElementById('date');
  console.log(elem.textContent.replace(/\D/g, '')); // 20210906
</script>
0
0
4

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