Last active
May 26, 2021 06:37
-
-
Save you21979/6219253 to your computer and use it in GitHub Desktop.
時間ライブラリmoment.jsの使い方
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var moment = require('moment'); | |
// ------------------------------------------ | |
// 初期化 | |
// ------------------------------------------ | |
// 現在時刻 | |
var today = moment(); | |
// unixタイムから求める | |
var fromUnixtTime = moment.unix(1368543600); | |
// 文字列 | |
var fromString = moment("May 15, 2013"); | |
// DATE | |
var fromDateObj = moment(new Date(2013, 4, 15)); | |
// array | |
var fromArray = moment([2013, 4, 15]); | |
// moment | |
var fromMoment = moment(today); | |
// format | |
var fromDDMMYYYY = moment("05-15-2013", "DD-MM-YYYY"); | |
var fromYYYYMMDD = moment("20130515", "YYYYMMDD"); | |
var fromYYYYMMDDJP = moment("2013年05月15日", "YYYY年MM月DD日"); | |
// UNIXTIME | |
var fromX = moment(1368543600 + "", "X"); | |
// コピー | |
var clone = today.clone(); | |
// ------------------------------------------ | |
// 演算 | |
// ------------------------------------------ | |
// 加算 | |
today.add('minutes', 1); | |
today.add('hours', 1); | |
today.add('days', 1); | |
// 減算 | |
fromX.subtract('minutes', 1); | |
fromX.subtract('hours', 1); | |
fromX.subtract('days', 1); | |
// ------------------------------------------ | |
// 表示 | |
// ------------------------------------------ | |
// 各種値 | |
console.log(today.year()); | |
console.log(today.month()); | |
console.log(today.date()); | |
console.log(today.day()); | |
console.log(today.hours()); | |
console.log(today.minutes()); | |
console.log(today.seconds()); | |
console.log(today.milliseconds()); | |
// YYYYMMDD HHMMSS | |
console.log(today.format("YYYY/MM/DD HH:mm:ss")); | |
// YYYYMMDD WEEK JP | |
console.log(today.format("YYYY年MM月DD日 ddd")); | |
// unixタイム | |
console.log(fromUnixtTime.unix()); | |
// 生の値 | |
console.log(fromUnixtTime.valueOf()); | |
console.log(fromUnixtTime.toJSON()); | |
console.log(today.format("YYYY/MM/DD HH:mm:ss")); | |
console.log(clone.format("YYYY/MM/DD HH:mm:ss")); | |
// 差分 | |
console.log(today.diff(clone)); | |
時間でイベントドリブンするライブラリとかないのかな
setTimeoutとか使いにくいと思うがそこまでいくとTimeSchedulerの領域かな
Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to moment/moment#1407 for more info.
Deprecation warning: moment().add(period, number) is deprecated. Please use moment().add(number, period).
Deprecation warning: moment().subtract(period, number) is deprecated. Please use moment().subtract(number, period).
2014
11
6
6
6
15
19
740
2014/12/06 06:15:19
2014年12月06日 Sat
1368543600
1368543600000
2013-05-14T15:00:00.000Z
2014/12/06 06:15:19
2014/12/05 05:14:19
90060000
久しぶりに実行したらdeprecateが出てた
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
使いやすいと思う