伙计们,

我正在尝试理解MomentJS API。获取机器上当前时间的适当方法是什么?

var CurrentDate = moment();

vs

var CurrentDate = moment().format();

试图解析他们的文档,它不明显使用什么。

http://momentjs.com/docs/#/query/is-a-moment/


当前回答

对于任何使用反应时刻的人:

import Moment from 'react-moment'

内部渲染(使用你需要的格式道具):

const now = new Date()
<Moment format="MM/DD/YYYY">{now}</Moment>

其他回答

我想补充一点,你可以在一个对象中拥有全部的数据信息:

const today = moment().toObject();

你应该获得一个具有这些属性的对象:

today: {
    date: 15,
    hours: 1,
    milliseconds: 927,
    minutes: 59,
    months: 4,
    seconds: 43,
    years: 2019
    }

当你必须计算日期时,它是非常有用的。

当前日期使用moment .js,格式为DD-MM-YYYY

const currentdate=moment().format("DD-MM-YYYY"); 
console.log(currentdate)

要使用am / pm和moment,下面是一个react组件的代码片段

import Moment from 'moment';

const time = Moment().format("hh:mm a")

作为参考,请记住Moment.js被认为是一个遗留项目,可能会带来性能开销

Moment ().unix()你会得到一个Unix时间戳(以秒为单位)

moment().valueOf()你将得到一个完整的时间戳(以毫秒为单位)

如果您只想要从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();