[javascript] How to return the current timestamp with Moment.js?

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/

This question is related to javascript node.js momentjs

The answer is


Get by Location:

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

Try to use it this way:

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

Try this

console.log(moment().format("MM ddd, YYYY HH:mm:ss a"));

_x000D_
_x000D_
console.log(moment().format("MM ddd, YYYY hh:mm:ss a"));
_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
_x000D_
_x000D_
_x000D_


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

I'm using this:

moment().toDate().getTime()

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.


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

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

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

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


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.


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

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


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>

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to node.js

Hide Signs that Meteor.js was Used Querying date field in MongoDB with Mongoose SyntaxError: Cannot use import statement outside a module Server Discovery And Monitoring engine is deprecated How to fix ReferenceError: primordials is not defined in node UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac internal/modules/cjs/loader.js:582 throw err DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean`

Examples related to momentjs

How to get am pm from the date time string using moment js Vuejs and Vue.set(), update array How to subtract one month using moment.js? Get timezone from users browser using moment(timezone).js Check if date is a valid one moment.js get current time in milliseconds? Deprecation warning in Moment.js - Not in a recognized ISO format Moment js get first and last day of current month Moment get current date Moment.js - How to convert date string into date?