[RAILS] Handle dates with Javascript (moment.js)


This is Qiita's first post. If you find something wrong, I would appreciate it if you could point it out.

Development environment

ruby 2.6.5 Rails


moment.js is a convenient library for handling dates with javascript. It is said that there are drawbacks in terms of package management, but I tried using it because it is still the mainstream technology.

how to use

Install moment.js in the directory you are working on

npm install moment

Load moment with the js file you want to use


var moment = require(“moment”);

↓ You can generate it in this way


const dt = new moment();

However, this alone will get stuck in rails s. ↓ You will probably need to reinstall 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 is False by default I'll just run yarn install --check-files

yarn install --check-files

I was able to use it without problems


moment.js is a legacy technology and may not be used much in the future. It is said that day.js is lighter and is upward compatible in terms of management, Since I may be active in active duty, I tried using it on a trial basis.

This person's article will be helpful when migrating. (I'm sorry to post it without permission) https://qiita.com/oika/items/2d15aea7809ab358ba25

Recommended Posts

Handle dates with Javascript (moment.js)
Handle files with NIO.2.
Handle JSON with minimal-json
Generate JavaScript with Thymeleaf
[Ruby] Handle instance variables with instance methods
Handle DatePicker with Ruby / gtk3 + glade3
Handle JSON effortlessly with Alamofire's respone Json