How to generate timestamp unix epoch format nodejs?

后端 未结 7 1700
感动是毒
感动是毒 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:39

    the AWS sdk includes utility functions for converting the amazon date format.

    For example, in a call back from an S3 get object, there is a property 'LastModified' that is in the amazon date format. (it appears they are doing nothing but exporting the standard Date class for their date properties such as S3 object 'LastModified' property) That format includes some utilities for various formats built in (unfortunately, none for unix epoch):

    let awsTime = response.LastModified
    console.log("Time Formats",{
        "String"           : awsTime.toString(),
        "JSON"             : awsTime.toJSON(),
        "UTCString"        : awsTime.toUTCString(),
        "TimeString"       : awsTime.toTimeString(),
        "DateString"       : awsTime.toDateString(),
        "ISOString"        : awsTime.toISOString(),
        "LocaleTimeString" : awsTime.toLocaleTimeString(),
        "LocaleDateString" : awsTime.toLocaleDateString(),
        "LocaleString"     : awsTime.toLocaleString()
    })
    /*
    Time Formats {
      String: 'Fri Sep 27 2019 16:54:31 GMT-0400 (EDT)',
      JSON: '2019-09-27T20:54:31.000Z',
      UTCString: 'Fri, 27 Sep 2019 20:54:31 GMT',
      TimeString: '16:54:31 GMT-0400 (EDT)',
      DateString: 'Fri Sep 27 2019',
      ISOString: '2019-09-27T20:54:31.000Z',
      LocaleTimeString: '16:54:31',
      LocaleDateString: '2019-9-27',
      LocaleString: '2019-9-27 16:54:31'
    }
    */
    

    However, the AWS utils function includes a 'date' module with other functions including a unixTimestamp method:

    let awsTime = response.LastModified
    let unixEpoch = Math.floor(AWS.util.date.unixTimestamp(awsTime))
    

    Note: this method returns a float value by default. Thus the Math.floor()

    Function code from aws-sdk.js (latest):

    /**
     * @return [Integer] the UNIX timestamp value for the current time
     */
     unixTimestamp: function unixTimestamp(date) {
         if (date === undefined) { date = util.date.getDate(); }
         return date.getTime() / 1000;
     }
    

    There are also methods for rfc822 and iso8601

提交回复
热议问题