How to return the current timestamp with Moment.js?

Javascriptnode.jsMomentjs

Javascript Problem Overview


Folks,

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

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/

Javascript Solutions


Solution 1 - Javascript

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 (https://stackoverflow.com/questions/522251/whats-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

Solution 2 - Javascript

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

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

Solution 3 - Javascript

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

I'm using this:

moment().toDate().getTime()

Solution 4 - Javascript

Try to use it this way:

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

Solution 5 - Javascript

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

Solution 6 - Javascript

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>

Solution 7 - Javascript

Get by Location:

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

Solution 8 - Javascript

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.

Solution 9 - Javascript

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

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

Solution 10 - Javascript

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.

Solution 11 - Javascript

To use am / pm with moment, here is a snippet from a react component

import Moment from 'moment';

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

For reference please remember Moment.js is considered a legacy project that can carry performance overhead enter image description here

Solution 12 - Javascript

to anyone who's using react-moment:

import Moment from 'react-moment'

inside render (use format prop to your needed format):

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCmagView Question on Stackoverflow
Solution 1 - JavascriptcomaView Answer on Stackoverflow
Solution 2 - JavascriptSabrina LuoView Answer on Stackoverflow
Solution 3 - JavascriptcatamphetamineView Answer on Stackoverflow
Solution 4 - JavascriptCraft4View Answer on Stackoverflow
Solution 5 - JavascripthgoeblView Answer on Stackoverflow
Solution 6 - JavascriptDurgpal SinghView Answer on Stackoverflow
Solution 7 - JavascriptDarlan DieterichView Answer on Stackoverflow
Solution 8 - Javascriptmay mijangosView Answer on Stackoverflow
Solution 9 - JavascriptJojo JosephView Answer on Stackoverflow
Solution 10 - JavascriptJ CView Answer on Stackoverflow
Solution 11 - JavascriptChrisView Answer on Stackoverflow
Solution 12 - JavascriptHiLuLiTView Answer on Stackoverflow