伙计们,
我正在尝试理解MomentJS API。获取机器上当前时间的适当方法是什么?
var CurrentDate = moment();
vs
var CurrentDate = moment().format();
试图解析他们的文档,它不明显使用什么。
http://momentjs.com/docs/#/query/is-a-moment/
伙计们,
我正在尝试理解MomentJS API。获取机器上当前时间的适当方法是什么?
var CurrentDate = moment();
vs
var CurrentDate = moment().format();
试图解析他们的文档,它不明显使用什么。
http://momentjs.com/docs/#/query/is-a-moment/
当前回答
试试这个方法:
console.log(moment().format('L'));
moment().format('L'); // 05/25/2018
moment().format('l'); // 5/25/2018
日期格式:
moment().format('MMMM Do YYYY, h:mm:ss a'); // May 25th 2018, 2:02:13 pm
moment().format('dddd'); // Friday
moment().format("MMM Do YY"); // May 25th 18
moment().format('YYYY [escaped] YYYY'); // 2018 escaped 2018
moment().format(); // 2018-05-25T14:02:13-05:00
访问:https://momentjs.com/了解更多信息。
其他回答
如果您只想要从01-JAN-1970年开始的毫秒数,那么您可以使用
var theMoment = moment(); // or whatever your moment instance is
var millis;
millis = +theMoment; // a short but not very readable form
// or
millis = theMoment.valueOf();
// or (almost sure not as efficient as above)
millis = theMoment.toDate().getTime();
我想补充一点,你可以在一个对象中拥有全部的数据信息:
const today = moment().toObject();
你应该获得一个具有这些属性的对象:
today: {
date: 15,
hours: 1,
milliseconds: 927,
minutes: 59,
months: 4,
seconds: 43,
years: 2019
}
当你必须计算日期时,它是非常有用的。
地点:
moment.locale('pt-br')
return moment().format('DD/MM/YYYY HH:mm:ss')
对于任何使用反应时刻的人:
import Moment from 'react-moment'
内部渲染(使用你需要的格式道具):
const now = new Date()
<Moment format="MM/DD/YYYY">{now}</Moment>
试着这样使用它:
let current_time = moment().format("HH:mm")