Javascript 如何使用 Moment.js 返回当前时间戳?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25734743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 21:47:34  来源:igfitidea点击:

How to return the current timestamp with Moment.js?

javascriptnode.jsmomentjs

提问by Cmag

Folks,

各位,

I am trying to understand the MomentJS API. What is the appropriate way to get the current time on the machine?

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

var CurrentDate = moment();

vs

对比

var CurrentDate = moment().format();

Trying to parse their docs, and its not apparent what to use.

试图解析他们的文档,但不清楚使用什么。

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

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

回答by coma

Here you are assigning an instance of momentjs to CurrentDate:

在这里,您将一个 momentjs 实例分配给 CurrentDate:

var CurrentDate = moment();

Here just a string, the result from default formatting of a momentjs instance:

这里只是一个字符串,来自 momentjs 实例的默认格式的结果:

var CurrentDate = moment().format();

And here the number of seconds since january of... well, unix timestamp:

这里是自 1 月以来的秒数......好吧,unix 时间戳:

var CurrentDate = moment().unix();

And here another string as ISO 8601 (What's the difference between ISO 8601 and RFC 3339 Date Formats?):

这里有另一个字符串作为 ISO 8601(ISO 8601 和 RFC 3339 日期格式有什么区别?):

var CurrentDate = moment().toISOString();

And this can be done too:

这也可以做到:

var a = moment();
var b = moment(a.toISOString());

console.log(a.isSame(b)); // true

回答by Sabrina Luo

moment().unix()you will get a unix timestamp

moment().unix()你会得到一个unix时间戳

moment().valueOf()you will get a full timestamp

moment().valueOf()你会得到一个完整的时间戳

回答by catamphetamine

Still, no answer. Moment.js - Can do anything but such a simple task.

仍然,没有答案。Moment.js - 除了这么简单的任务,什么都可以做。

I'm using this:

我正在使用这个:

moment().toDate().getTime()

回答by Craft4

Try to use it this way:

尝试以这种方式使用它:

let current_time = moment().format("HH:mm")

回答by hgoebl

If you just want the milliseconds since 01-JAN-1970, then you can use

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

回答by Durgpal Singh

Try this

尝试这个

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>

回答by may mijangos

Try this way:

试试这个方法:

console.log(moment().format('L'));
moment().format('L');    // 05/25/2018
moment().format('l');    // 5/25/2018

Format Dates:

格式日期:

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

Visit: https://momentjs.com/for more info.

访问:https: //momentjs.com/了解更多信息。

回答by J C

I would like to add that you can have the whole data information in an object with:

我想补充一点,您可以将整个数据信息放在一个对象中:

const today = moment().toObject();

You should obtain an object with this properties:

您应该获得具有以下属性的对象:

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

It is very useful when you have to calculate dates.

当您必须计算日期时,它非常有用。

回答by Darlan Dieterich

Get by Location:

按地点获取:

moment.locale('pt-br')
return moment().format('DD/MM/YYYY HH:mm:ss')

回答by Jojo Joseph

Current date using momment.js in DD-MM-YYYY format

当前日期使用 DD-MM-YYYY 格式的 momment.js

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