Help us understand the problem. What is going on with this article?

Node.jsで現在時刻を取得

More than 5 years have passed since last update.

Javascriptの標準関数でやろうとすると案外めんどくさいのでNode.jsのモジュールで良いものが無いか探したらdate-utilsというのがあったので使ってみました。

JerrySievert/node-date-utils

npmでインストール

$ npm install date-utils

すぐに入ります。

使いたい場所で呼び出す

これだけ。
めっちゃ簡単でした。

index.js
require('date-utils');

var dt = new Date();
var formatted = dt.toFormat("YYYYMMDDHH24MISS");
console.log(formatted);
20140213221933

ちなみに自由に区切り文字を入れられるので
dt.toFormat("YYYY/MM/DD HH24時MI分SS秒");とかにすれば,2014/02/13 22時29分06秒と出力されます.

使ってる(サーバー)マシンで指定されている時間を呼び出すので,表示される時間がおかしい時はマシン設定を見てみると良いと思います.

指定の仕方

toFormat()というメソッドで割と色々な形で時刻を取得できます.

date-utilsのREADME.mdより
d.toFormat(format); // returns date formatted with:
  // YYYY - Four digit year
  // MMMM - Full month name. ie January
  // MMM  - Short month name. ie Jan
  // MM   - Zero padded month ie 01
  // M    - Month ie 1
  // DDDD - Full day or week name ie Tuesday 
  // DDD  - Abbreviated day of the week ie Tue
  // DD   - Zero padded day ie 08
  // D    - Day ie 8
  // HH24 - Hours in 24 notation ie 18
  // HH   - Padded Hours ie 06
  // H    - Hours ie 6
  // MI   - Padded Minutes
  // SS   - Padded Seconds
  // PP   - AM or PM
  // P    - am or pm

参考日付型をフォーマットしたりする - dunno logs

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away