moment.min.js_Moment.js教程

moment.min.js

Moment.js is an awesome JavaScript library that helps you manage dates, in the browser and in Node.js as well.

Moment.js是一个很棒JavaScript库,可帮助您在浏览器和Node.js中管理日期。

This article aims to explain the basics and the most common usages of this library.

本文旨在解释该库的基础知识和最常用的用法。

安装 (Installation)

You can include it directly in your page using a script tag, from unpkg.com:

您可以使用脚本标签直接从unpkg.com将其包含在页面中:

<script src="https://unpkg.com/moment" />

or using npm:

或使用npm

npm install moment

If you install using npm you need to import the package (using ES Modules):

如果使用npm安装,则需要导入软件包(使用ES Modules ):

import moment from 'moment'

or require it (using CommonJS):

或要求它(使用CommonJS ):

const moment = require('moment')

获取当前日期和时间 (Get the current date and time)

const date = moment()

解析日期 (Parse a date)

A moment object can be initialized with a date by passing it a string:

可以通过将一个字符串对象传递给一个日期来对其进行初始化:

const date = moment(string)

it accepts any string, parsed according to (in order):

它接受根据(按顺序)解析的任何字符串:

ISO 8601 is definitely the most convenient. Here’s a format reference:

ISO 8601绝对是最方便的。 这是格式参考:

FormatMeaningExample
YYYY4-digits Year2018
YY2-digits Year18
M2-digits Month number, omits leading 07
MM2-digits Month number07
MMM3-letters Month nameJul
MMMMFull Month nameJuly
ddddFull day nameSunday
gggg4-digits Week year2018
gg2-digits Week year18
wWeek of the year without leading zero18
wwWeek of the year with leading zero18
eDay of the week, starts at 04
D2-digits day number, omits leading 09
DD2-digits day number09
DoDay number with ordinal9th
TIndicates the start of the time part
HH2-digits hours (24 hour time) from 0 to 2322
H2-digits hours (24 hour time) from 0 to 23 without leading 022
kk2-digits hours (24 hour time) from 1 to 2423
k2-digits hours (24 hour time) from 1 to 24 without leading 023
a/Aam or pmpm
hh2-digits hours (12 hour time)11
mm2-digits minutes22
ss2-digits seconds40
s2-digits seconds without leading zero40
S1-digits milliseconds1
SS2-digits milliseconds12
SSS3-digits milliseconds123
ZThe timezone+02:00
xUNIX timestamp in milliseconds1410432140575
格式 含义
YYYY 4位数字年份 2018年
YY 两位数年份 18
中号 2位数字月份号,省略前导0 7
MM 2位数字月份号 07
MMM 3个字母的月份名称 七月
MMMM 全月名称 七月
dddd 全日名称 星期日
gggg 4位数字的周年 2018年
gg 2位数字的星期年 18
w 一年中的星期,不带零 18
w 一年中的前零周 18
Ë 星期几,从0开始 4
d 2位日数字,省略前导0 9
DD 2位数的日号 09
带序号的天数 第九名
Ť 指示时间部分的开始
H 2位数小时(24小时制),从0到23 22
H 2位数小时(24小时制),从0到23,前导0 22
kk 2位数小时(24小时制),从1到24 23
ķ 2位数小时(24小时制),从1到24,不加0 23
A / A ampm 下午
h 2位数小时(12小时制) 11
毫米 2位数字分钟 22
ss 2位数秒 40
s 2位秒,不带前导零 40
小号 1位数毫秒 1个
SS 2位毫秒 12
SSS 3位数毫秒 123
ž 时区 +02:00
X UNIX时间戳(以毫秒为单位) 1410432140575

设定日期 (Set a date)

格式化日期 (Format a date)

When you want to output the content of a plain JavaScript Date object, you have little options to determine the formatting. All you can do is to use the built-in methods, and compose the date as you want using them.

当您要输出纯JavaScript Date对象的内容时,几乎没有选择来确定格式。 您所能做的就是使用内置方法,并根据需要编写日期。

Moment offers a handy way to format the date according to your needs, using the format() method:

Moment提供了一种方便的方式,可以使用format()方法根据您的需要格式化日期:

date.format(string)

The string format accepts the same formats I described in the “Parse a date” section above.

字符串格式接受与我上面“解析日期”一节中描述的格式相同的格式。

Example:

例:

moment().format("YYYY Do MM")

Moment provides some constants you can use instead of writing your own format:

Moment提供了一些常量,您可以使用它们来代替编写自己的格式:

ConstantFormatExample
moment.HTML5_FMT.DATETIME_LOCALYYYY-MM-DDTHH:mm2017-12-14T16:34
moment.HTML5_FMT.DATETIME_LOCAL_SECONDSYYYY-MM-DDTHH:mm:ss2017-12-14T16:34:10
moment.HTML5_FMT.DATETIME_LOCAL_MSYYYY-MM-DDTHH:mm:ss.SSS2017-12-14T16:34:10.234
moment.HTML5_FMT.DATEYYYY-MM-DD2017-12-14
moment.HTML5_FMT.TIMEHH:mm16:34
moment.HTML5_FMT.TIME_SECONDSHH:mm:ss16:34:10
moment.HTML5_FMT.TIME_MSHH:mm:ss.SSS16:34:10.234
moment.HTML5_FMT.WEEKYYYY-[W]WW2017-W50
moment.HTML5_FMT.MONTHYYYY-MM2017-12
不变 格式
moment.HTML5_FMT.DATETIME_LOCAL YYYY-MM-DDTHH:mm 2017-12-14T16:34
moment.HTML5_FMT.DATETIME_LOCAL_SECONDS YYYY-MM-DDTHH:mm:ss 2017-12-14T16:34:10
moment.HTML5_FMT.DATETIME_LOCAL_MS YYYY-MM-DDTHH:mm:ss.SSS 2017-12-14T16:34:10.234
moment.HTML5_FMT.DATE YYYY-MM-DD 2017-12-14
moment.HTML5_FMT.TIME 高度:毫米 16:34
moment.HTML5_FMT.TIME_SECONDS HH:mm:ss 16:34:10
moment.HTML5_FMT.TIME_MS HH:mm:ss.SSS 16:34:10.234
moment.HTML5_FMT.WEEK YYYY- [W] WW 2017-W50
moment.HTML5_FMT.MONTH YYYY-MM 2017-12

验证日期 (Validating a date)

Any date can be checked for validity using the isValid() method:

可以使用isValid()方法检查任何日期的有效性:

moment('2018-13-23').isValid() //false
moment('2018-11-23').isValid() //true

时间以前,直到日期 (Time ago, time until date)

Use fromNow(). Strings are localized:

使用fromNow() 。 字符串已本地化:

moment('2016-11-23').fromNow() //2 years ago
moment('2018-05-23').fromNow() //a month ago
moment('2018-11-23').fromNow() //in 5 months

if you pass true to fromNow(), it just shows the difference, without reference to future/past.

如果将true传递给fromNow(),它将仅显示差异,而不会引用future / past。

moment('2016-11-23').fromNow(true) //2 years
moment('2018-05-23').fromNow(true) //a month
moment('2018-11-23').fromNow(true) //5 months

处理日期 (Manipulate a date)

You can add or subtract any amount of time to a date:

您可以在日期中增加或减少任何时间:

moment('2016-11-23').add(1, 'years')
moment('2016-11-23').subtract(1, 'years')

You can use those values:

您可以使用这些值:

  • years

    years

  • quarters

    quarters

  • months

    months

  • weeks

    weeks

  • days

    days

  • hours

    hours

  • minutes

    minutes

  • seconds

    seconds

  • milliseconds

    milliseconds

翻译自: https://flaviocopes.com/momentjs/

moment.min.js

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值