How to return the current timestamp with Moment.js?

前端 未结 11 1662
佛祖请我去吃肉
佛祖请我去吃肉 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:35

    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

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

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

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

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

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

    Try this

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

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

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

    Try to use it this way:

    let current_time = moment().format("HH:mm")
    
    0 讨论(0)
  • 2020-12-23 00:44

    Get by Location:

    moment.locale('pt-br')
    return moment().format('DD/MM/YYYY HH:mm:ss')
    
    0 讨论(0)
提交回复
热议问题