时刻js从字符串中提取年份

Mau*_*o74 19 javascript date momentjs

我是moment.js的新手,我无法理解文档.我想以字符串格式操作一些日期.

我的日期是来自json feed的字符串,看起来像:

var year = item.date
// it returns a string like "25/04/2012"
Run Code Online (Sandbox Code Playgroud)

如何使用moment.js从中提取年份?

Luc*_*uca 51

您可以使用

moment("25/04/2012","DD/MM/YYYY").format("YYYY")
or    
moment("25/04/2012","DD/MM/YYYY").year()
Run Code Online (Sandbox Code Playgroud)

在你的例子中:

moment(item.date,"DD/MM/YYYY").year()
Run Code Online (Sandbox Code Playgroud)