How to return the current timestamp with Moment.js?

前端 未结 11 1663
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-23 00:28

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


        
相关标签:
11条回答
  • 2020-12-23 00:47

    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('ffffdd');                    // 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.

    0 讨论(0)
  • 2020-12-23 00:52

    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
    
    0 讨论(0)
  • 2020-12-23 00:52

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

    I'm using this:

    moment().toDate().getTime()
    
    0 讨论(0)
  • 2020-12-23 00:53

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

    const currentdate=moment().format("DD-MM-YYYY"); 
    console.log(currentdate)
    
    0 讨论(0)
  • 2020-12-23 00:59

    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.

    0 讨论(0)
提交回复
热议问题