Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return the current timestamp with Moment.js?

People also ask

What is Moment () hour ()?

The moment(). hour() Method is used to get the hours from the current time or to set the hours. Syntax: moment().hour(); or. moment().

What does Moment now () return?

var now = moment(); This is essentially the same as calling moment(new Date()) . Note: From version 2.14.0, moment([]) and moment({}) also return now. They used to default to start-of-today before 2.14.0, but that was arbitrary so it was changed.

How do I get a timestamp in my Moment?

If you are creating timestamps using built-in new Date() , you can also use it to create moment objects: const timestamp = new Date(); const momentTimestamp = moment(timestamp); If you want to create a moment object using current date and time, just call moment() without any arguments.


Here you are assigning an instance of momentjs to CurrentDate:

var CurrentDate = moment();

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

var CurrentDate = moment().format();

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

var CurrentDate = moment().unix();

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

var CurrentDate = moment().toISOString();

And this can be done too:

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

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

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

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


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

I'm using this:

moment().toDate().getTime()

Try to use it this way:

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

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

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();

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>