How to generate timestamp unix epoch format nodejs?

后端 未结 7 1695
感动是毒
感动是毒 2021-01-31 06:54

I am trying to send data to graphite carbon-cache process on port 2003 using

Ubuntu terminal:

echo "test.average 4 `date +%s`" | nc -q0 127.0.0.1         


        
7条回答
  •  说谎
    说谎 (楼主)
    2021-01-31 07:40

    Helper methods that simplifies it, copy/paste the following on top of your JS:

    Date.prototype.toUnixTime = function() { return this.getTime()/1000|0 };
    Date.time = function() { return new Date().toUnixTime(); }
    

    Now you can use it wherever you want by simple calls:

    // Get the current unix time: 
    console.log(Date.time())
    
    // Parse a date and get it as Unix time
    console.log(new Date('Mon, 25 Dec 2010 13:30:00 GMT').toUnixTime())
    

    Demo:

         
        Date.prototype.toUnixTime = function() { return this.getTime()/1000|0 };
        Date.time = function() { return new Date().toUnixTime(); }
    
        // Get the current unix time: 
        console.log("Current Time: " + Date.time())
    
        // Parse a date and get it as Unix time
        console.log("Custom Time (Mon, 25 Dec 2010 13:30:00 GMT): " + new Date('Mon, 25 Dec 2010 13:30:00 GMT').toUnixTime())

提交回复
热议问题