LoginSignup
5
7

More than 5 years have passed since last update.

Bluemix Node-REDに2行追加して日本時間を簡単に出力する

Last updated at Posted at 2017-01-05

経緯

Bluemixのリージョンに日本(とタイムゾーンの同じ地域)がないため、単純に日付が出力できない。
調べると、Moment.jsの利用で解決できそうなので、Node-REDでの利用方法を含めてまとめた。

前提

内容

  1. package.jsonにMoment.jsの依存関係を追加する

    package.json(修正前)
    "dependencies": {
            :
        "node-red-nodes-cf-sqldb-dashdb":"0.x"
    },
    
    package.json(修正後)
    "dependencies": {
            :
        "node-red-nodes-cf-sqldb-dashdb":"0.x",
        "moment": "^2.17.1"
    },
    

    注意)"node-red-nodes-cf-sqldb-dashdb":"0.x"行の末尾にカンマを忘れない。

  2. bluemix-settings.jsにmomentをrequireする設定を記載する

    bluemix-setting.js(修正前)
        functionGlobalContext: { },
    
    bluemix-setting.js(修正後)
        functionGlobalContext: {
            moment : require("moment")
        },
    

    補足)functionGlobalContextrequireを記載すると、function node内でglobal.contextから利用できるようになる

  3. Deploy

    • Eclipse Orion Web IDEを用いている場合は、「ワークスペースからアプリケーションのデプロイ」ボタンを押す
    • GitHub&Delivery Pipelineを利用している場合は、コミット&プッシュする(自動的にデプロイされる)

利用

Node-REDのFlowでfunction nodeを追加して、以下のようにmomentを利用する

var date = context.global.moment();
date.locale("ja").tz("Asia/Tokyo"); // ロケールを日本語、タイムゾーンを東京に設定
msg.payload = date.format("LLL");
return msg;

Flowイメージ
スクリーンショット 2017-01-05 23.10.54.png

Debug出力
スクリーンショット 2017-01-05 23.12.15.png

備考

Moment.jsの詳細については、http://momentjs.com を参照

5
7
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
5
7