一、下載並安裝NodeJS
官網地址:https://nodejs.org/en/
64位Windows用戶可以直接點擊如下右邊的按鈕進行下載安裝,其他用戶可以點擊如下 Other Downloads 鏈接下載相關版本的node
檢驗是否安裝成功,Windows鍵+R,打開命令行窗口,輸入如下,如若輸出有版本號,表示node安裝完畢,npm(Node Package Manager)安裝完畢
二、新建項目
結果如下:
三、編寫項目代碼
新建 index.js 文件,輸入如下代碼
var moment = require('moment-timezone') function testFormatZ(dt, tzName) { var noZ = moment.tz(dt, tzName); var hasZ = moment.tz(dt, tzName).format('Z') console.log(dt) console.log(noZ) console.log(hasZ) }; testFormatZ(new Date(), 'Asia/Hong_Kong');
以上代碼是用來獲取一定格式的日期,用到了moment-timezone 插件,命令行中輸入npm install moment-timezone --save,進行下載 (需要什么就下載什么)
下載完成之后,輸入 node index 運行代碼(運行node項目就是 node+文件名)
NOTE: Node很強大,這只是一個很小很小的項目,任何你想得到的,Node都可能實現,值得進一步探索
When there is a will, there is a way!