2
0

More than 3 years have passed since last update.

Javascriptで日付を扱う(moment.js)

Last updated at Posted at 2020-10-17

はじめに

Qiita初投稿です。
間違っていることがありましたら、ご指摘いただけると幸いです。

開発環境

ruby 2.6.5
Rails 6.0.3.3

概要

moment.jsとはjavascriptで日付を扱うときに便利なライブラリです。
パッケージ管理面での欠点があるとも言われていますが、まだ主流な技術なので使用してみました。 

使用方法

作業中のディレクトリでmoment.jsをインストールします

npm install moment

使用したいjsファイルでmomentを読み込みます

〇〇.js
var moment = require(moment);

↓このような書き方で生成できます

〇〇.js
const dt = new moment();

しかし、これだけだとrails sで引っかかります。
↓おそらく、yarnの再インストールが必要になります。

error Lockfile does not contain pattern: "moment@^2.29.1"                                                                                                              
error Found 1 errors.                                                                                                                                                  


========================================
  Your Yarn packages are out of date!
  Please run `yarn install --check-files` to update.
========================================


To disable this check, please change `check_yarn_integrity`
to `false` in your webpacker config file (config/webpacker.yml).

check_yarn_integrityはデフォルトでFalseとなっているので
yarn install --check-filesだけを実行してみます

yarn install --check-files

問題なく使うことができました

最後に

moment.jsはレガシーな技術で今後はあまり使われなくなるかも知れません。
day.jsの方が軽量で管理面でも上位互換だとも言われていますが,
現役で活躍していることもあるので試験的に使用してみました。

移行する際は、この方の記事が参考になります。(勝手に掲載してすいません)
https://qiita.com/oika/items/2d15aea7809ab358ba25

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