266

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

I'm trying to understand their docs, and it's not apparent what to use.

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

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Cmag
  • 14,946
  • 25
  • 89
  • 140
  • 3
    The current time as what? unix timestamp? – coma Sep 08 '14 at 23:48
  • I would imagine the RFC3339 or whatever the internet standard id for returning back to clients, etc – Cmag Sep 08 '14 at 23:54
  • 2
    Returning back to the client... hummm... I guess that you are getting it in nodejs and sending it to the browser wanting to convert it again to a momentjs instance? – coma Sep 08 '14 at 23:57
  • 1
    Sorry, have you found my answer helpful? – coma Sep 09 '14 at 14:27
  • 1
    absolutely, i think :) asked a quick question regarding `var now = new Date().getTime(); userObject.registered = moment(now).toDate();` – Cmag Sep 09 '14 at 20:48

12 Answers12

373

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
Community
  • 1
  • 1
coma
  • 16,429
  • 4
  • 51
  • 76
  • would this be acceptable? `var now = new Date().getTime(); userObject.registered = moment(now).toDate();` – Cmag Sep 09 '14 at 20:48
  • 2
    @Cmag, I don't understand the question, but just take a look at this: http://jsfiddle.net/coma/bLe31qq0/ , as you can see there, even after a second the moment instance keeps the same time since it's frozen, is the same behaviour as new Date(). – coma Sep 09 '14 at 21:20
  • so its the same behavior? – Cmag Sep 09 '14 at 22:32
  • 2
    @Cmag, Yes, it is, think of momentjs as a Date object on steroids and use the toDate method only when you are working with third party libs needing a raw Date object. With momentjs, complex time calculations are more than easy, are pleasant. I crafted a datepicker with it: https://github.com/coma/MomentPicker/blob/master/src/MomentPicker.js – coma Sep 10 '14 at 07:14
  • Also the `.format('x')` option will generate a timestamp string. – Jimmy Knoot Dec 16 '16 at 12:35
  • Answered the first question AND the follow-up question I had when faced with a deprecation warning. Brilliant, thanks! – CosetteN Sep 29 '20 at 21:55
135

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

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

Sabrina Luo
  • 3,810
  • 4
  • 16
  • 35
  • 1
    To format ` var CurrentDateUnixTimestamp = moment().unix(); moment.unix(CurrentDateUnixTimestamp).format("YYYY-MM-DD HH:mm");` – Glitch Jun 11 '19 at 13:37
  • What's difference between `unix` timestamp and `full` timestamp? I suppose the `full` timestamp is what indicates the ~time flow~ in milliseconds since 1970 Jan 1 but I can't figure out the other one. – DongBin Kim Sep 17 '19 at 06:06
  • 6
    `.unix()` gives you the Unix timestamp in seconds, `.valueOf()` gives you the Unix timestamp in milliseconds (AKA what I've always thought of as a Unix timestamp). – Andrew Koster Sep 30 '19 at 02:53
  • 1
    @AndrewKoster I would point out that the UNIX timestamp (POSIX) is actually defined as the number of UTC *seconds* elapsed since 1/1/1970 00:00:00 ignoring leap seconds. So it's actually redundant to say "unix timestamp in seconds" and the recent obsession with using milliseconds causes me no end of frustration. One should only use milliseconds when it is necessary to the application. Most applications do not need this granularity and I was disappointed when I found out that moment js `valueOf` uses millis because it is reinforcing a bad pattern imo. – JeneralJames Mar 31 '21 at 12:41
  • i.e what you always thought of as UNIX timestamp is actually incorrect. You have them backwards. There is a reason why moment js calls the function `unix`... – JeneralJames Mar 31 '21 at 12:43
  • Yeah, Moment is right to call the seconds one `unix`, I'm the one who has it backwards in my head. If anything, they should rename `valueOf` to something involving milliseconds (i.e. `unixMilliseconds`). – Andrew Koster Apr 01 '21 at 02:30
42

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

I'm using this:

moment().toDate().getTime()
catamphetamine
  • 4,489
  • 30
  • 25
42

Try to use it this way:

let current_time = moment().format("HH:mm")
Idan
  • 3,604
  • 1
  • 28
  • 33
Craft4
  • 429
  • 4
  • 2
18

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();
hgoebl
  • 12,637
  • 9
  • 49
  • 72
13

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>
Dev
  • 3,922
  • 3
  • 24
  • 44
Durgpal Singh
  • 11,481
  • 4
  • 37
  • 49
6

Get by Location:

moment.locale('pt-br')
return moment().format('DD/MM/YYYY HH:mm:ss')
Darlan Dieterich
  • 2,369
  • 1
  • 27
  • 37
5

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.

Grant Miller
  • 27,532
  • 16
  • 147
  • 165
may mijangos
  • 69
  • 1
  • 3
4

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

const currentdate=moment().format("DD-MM-YYYY"); 
console.log(currentdate)
Jojo Joseph
  • 1,493
  • 1
  • 15
  • 12
3

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>
HiLuLiT
  • 433
  • 4
  • 7
1

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.

J C
  • 731
  • 7
  • 11
1

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

Chris
  • 18,075
  • 15
  • 59
  • 77