How to send request in Timestamp: yyyyMMddHHmmssffff format using Postman?

Asked

Viewed 1,704 times

2

I need to use Postman to send a request to the API and in the header I need to send the date in format yyyyMMddHHmmssffff.

I tried to use Moment.js this way, but it doesn’t seem to work:

const moment = require('moment');
pm.environment.set("timestamp", moment().format('YYYYMMDDHHmmssffff'));

2 answers

3

In the documentation of Moment.js we can see that for the fractions of a second S (the letter "S" uppercase), so the format would look like this:

console.log(moment().format('YYYYMMDDHHmmssSSSS'));
<script src="https://momentjs.com/downloads/moment.min.js"></script>

In the same documentation we see that the letter f is not in the list of recognized formats.

This is an important point when using different date formatting languages and API’s. The letters corresponding to each field (year, month, day, hour, minute, second, etc.) are not necessarily the same, each one uses its own "pattern" (just compare Moment.js with PHP, Python and Java, for example). It is therefore important to always consult the documentation.

  • So I believe that as soon as it returns in the SSS the milliseconds, but from what it seems here in my problem somehow I need something smaller than the millisecond, in C# there is a function that gives exactly what I need, but I do not know how to put this result in my request. Console.Writeline(System.DateTime.Utcnow.Tostring("yyMMddHHmmssffff"));

  • 1

    @Paulovitor O Date Javascript has millisecond accuracy, so if you want 4 decimal places, the last one will always be zero. If the service only accepts 4 decimal places, use SSSS. If this is not the problem, please edit the question by asking more details

0

To get to the format you need it is possible to use only the methods of Date as the example below shows:

const a = new Date();
const yyyy = a.getFullYear();
const MM = (a.getMonth() + 1).toString().padStart(2, '0');
const dd = a.getDate().toString().padStart(2, '0');
const hh = a.getHours().toString().padStart(2, '0');
const mm = a.getMinutes().toString().padStart(2, '0');
const ss = a.getSeconds().toString().padStart(2, '0');
const ffff = a.getMilliseconds().toString().padStart(4, '0');

console.log(`${yyyy}${MM}${dd}${hh}${mm}${ss}${ffff}`);

To pass a value in the header:

const echoPostRequest = {
  url: 'https://postman-echo.com/post',
  method: 'POST',
  header: '<seu_cabeçalho>:<valor_data_formatada>', // << add seu valor aqui
  body: {
    mode: 'raw',
    raw: JSON.stringify({ key: 'this is json' })
  }
};

pm.sendRequest(echoPostRequest, function (err, res) {
  console.log(err ? err : res.json());
});
  • So I believe that so it returns in ffff the milliseconds and complete with zero left, but from what it seems here in my problem somehow I need something smaller than the millisecond, in C# there is a function that gives exactly what I need, but I don’t know how to put this result in my request. Console.Writeline(System.DateTime.Utcnow.Tostring("yyMMddHHmmssffff"));

  • 1

    @Paulovitor have a look at this JS code that matches . NET: https://gist.github.com/marlun78/1351171/5c10ff750547808a7c72d1953fefb5430c4829a5

Browser other questions tagged

You are not signed in. Login or sign up in order to post.