一个轻量级的 JavaScript 解析,验证,操作,和展示日期库。
Moment 被设计成既能在浏览器中使用,也能在 Node.js 中使用。
目前,以下浏览器被用于CI系统: IE8, Windows 7 上的 IE9 ,Windows XP 上的稳定版本的Chrome, Mac 上的 Safari 10.8 以及 Linux 上的稳定版本的Firefox。
所有的代码和单元测试都能在两种环境下运行。
在哪里使用
Node.js
npm install moment
var moment = require('moment');
moment().format();
注意:在 2.4.0
版本中,全局导出的 moment 对象是不建议使用的。这将在下一主要版本中被删除。
浏览器
<script src="moment.js"></script>
<script>
moment().format();
</script>
Moment.js 可以通过 cdnjs.com 获得,但要记住,可以通过合并JS文件尽量减少HTTP请求。
Bower
bower install --save moment
需要注意的文件是 moment.js
, locale/*.js
和 min/moment-with-locales.js
。
Require.js
require.config({
paths: {
"moment": "path/to/moment",
}
});
define(["moment"], function (moment) {
moment().format();
});
NuGet
Install-Package Moment.js
spm
Static Package Manager / moment
spm install moment --save
meteor
meteor / atmosphere / momentjs:moment
meteor add momentjs:moment
其它
在 Java/Rhino 下使用,请查看 these instructions。
在 Demandware 中使用,请查看 these instructions。
疑难解答
如果你有任何的问题,请在 Stack Overflow 上通过 momentjs
标签提问。
你也可以使用 GitHub issue tracker 来查找相关的 issues 和打开一个新的 issue。
下载
直接下载库文件
http://momentjs.com/downloads/moment.js
http://momentjs.com/downloads/moment.min.js
带有 locales 的文件
http://momentjs.com/downloads/moment-with-locales.js
http://momentjs.com/downloads/moment-with-locales.min.js
通过包管理器下载
bower install moment --save # bower
npm install moment --save # npm
Install-Package Moment.js # NuGet
spm install moment --save # spm
meteor add momentjs:moment # meteor
如何使用
格式化日期
moment().format('MMMM Do YYYY, h:mm:ss a'); //=> September 8th 2015, 9:45:36 pm
moment().format('dddd');//=> Tuesday
moment().format("MMM Do YY");//=> Sep 8th 15
moment().format('YYYY [escaped] YYYY'); //=> 2015 escaped 2015
moment().format(); //=> 2015-09-08T21:46:43+08:00
相对时间
moment("20111031", "YYYYMMDD").fromNow(); //=> 4 years ago
moment("20120620", "YYYYMMDD").fromNow(); //=>3 years ago
moment().startOf('day').fromNow(); //=>a day ago
moment().endOf('day').fromNow(); //=>in 2 hours
moment().startOf('hour').fromNow(); //=>an hour ago
日历时间
moment().subtract(10, 'days').calendar(); //=>08/29/2015
moment().subtract(6, 'days').calendar(); //=>Last Wednesday at 9:50 PM
moment().subtract(3, 'days').calendar(); //=>Last Saturday at 9:50 PM
moment().subtract(1, 'days').calendar(); //=>Yesterday at 9:50 PM
moment().calendar(); //=>Today at 9:50 PM
moment().add(1, 'days').calendar(); //=>Tomorrow at 9:51 PM
moment().add(3, 'days').calendar(); //=>Friday at 9:51 PM
moment().add(10, 'days').calendar(); //=>09/18/2015
多语言支持
Moment.js 拥有强大的国际化支持。你可以加载多个本地化文件并且在他们之间轻易切换。除了指定全局区域外,还可以给某个特定的 moment 实例指定一个区域。
全局更改区域
// 从 2.8.1 开始
moment.locale(String);
moment.locale(String[]);
moment.locale(String, Object);
// 在 2.8.1 中不建议使用
moment.lang(String);
moment.lang(String[]);
moment.lang(String, Object);
更多文档请查看:http://momentjs.com/docs
Port to ES6 (version 2.10.0)
Moment 2.10.0 does not bring any new features, but the code is now written in
es6 modules and placed inside src/
. Previously moment.js
, locale/*.js
and
test/moment/*.js
, test/locale/*.js
contained the source of the project. Now
the source is in src/
, temporary build (es5) files are placed under
build/umd/
(for running tests during development), and the moment.js
and
locale/*.js
files are updated only on release.
If you want to use a particular revision of the code, make sure to run
grunt transpile update-index
, so moment.js
and locales/*.js
are synced
with src/*
. We might place that in a commit hook in the future.
Upgrading to 2.0.0
There are a number of small backwards incompatible changes with version 2.0.0. See the full descriptions here
-
Changed language ordinal method to return the number + ordinal instead of just the ordinal.
-
Changed two digit year parsing cutoff to match strptime.
-
Removed
moment#sod
andmoment#eod
in favor ofmoment#startOf
andmoment#endOf
. -
Removed
moment.humanizeDuration()
in favor ofmoment.duration().humanize()
. -
Removed the lang data objects from the top level namespace.
-
Duplicate
Date
passed tomoment()
instead of referencing it.
更新日志
贡献
我们正在寻找共同的维护者!如果你想变成时间的主人,请给 ichernev 写信。
许可
Moment.js 是在 MIT license 下许可的。
评论