民间,
我正在尝试了解MomentJS API。什么是获取机器当前时间的适当方法?
var CurrentDate = moment();
VS
var CurrentDate = moment().format();
尝试解析他们的文档,并且不明白要使用什么。
答案 0 :(得分:156)
此处您正在为CurrentDate分配一个momentjs实例:
var CurrentDate = moment();
这里只是一个字符串,来自momentjs实例的默认格式化结果:
var CurrentDate = moment().format();
这里是自1月以来的秒数......好吧,unix时间戳:
var CurrentDate = moment().unix();
此处另一个字符串为ISO 8601(What's the difference between ISO 8601 and RFC 3339 Date Formats?):
var CurrentDate = moment().toISOString();
这也可以做到:
var a = moment();
var b = moment(a.toISOString());
console.log(a.isSame(b)); // true
答案 1 :(得分:41)
moment().unix()
您将获得一个unix时间戳
moment().valueOf()
您将获得一个完整的时间戳
答案 2 :(得分:27)
仍然没有答案。 Moment.js - 除了这么简单的任务外,什么也做不了。
我正在使用它:
moment().toDate().getTime()
答案 3 :(得分:24)
尝试以这种方式使用它:
var current_time = new moment().format("HH:mm");
答案 4 :(得分:11)
如果您只想要自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();
答案 5 :(得分:4)
试试这个
console.log(moment().format("MM ddd, YYYY hh:mm:ss a"));
console.log(moment().format("MM ddd, YYYY hh:mm:ss a"));

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
&#13;
答案 6 :(得分:1)
尝试这种方式:
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/了解详情。
答案 7 :(得分:1)
我想补充一点,您可以使用以下命令将整个数据信息包含在一个对象中:
const today = moment().toObject();
您应该获得具有以下属性的对象:
today: {
date: 15,
hours: 1,
milliseconds: 927,
minutes: 59,
months: 4,
seconds: 43,
years: 2019
}
当您必须计算日期时,它非常有用。
答案 8 :(得分:1)
致任何使用 react-moment
的人:
import Moment from 'react-moment'
内部渲染(使用 format
道具到您需要的格式):
const now = new Date()
<Moment format="MM/DD/YYYY">{now}</Moment>
答案 9 :(得分:0)
获取地点:
moment.locale('pt-br')
return moment().format('DD/MM/YYYY HH:mm:ss')
答案 10 :(得分:0)
使用momment.js的当前日期为DD-MM-YYYY格式
const currentdate=moment().format("DD-MM-YYYY");
console.log(currentdate)
答案 11 :(得分:0)
要在瞬间使用am / pm,这是react组件的一个片段
import Moment from 'moment';
const time = Moment().format("hh:mm a")
作为参考,请记住Moment.js is considered a legacy project可能会带来性能开销