-3

So this is my code:

var input = '2015-07-13T22:00:00Z';
var output = encodeURIComponent(input);

console.log(output);

How can i use encodeURIComponent in momentjs?

Thanks.

  • This is a Javascript method : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent – Hyyan Abo Fakher May 07 '18 at 13:22
  • Why would you do that? Do you understand what they do? – evolutionxbox May 07 '18 at 13:22
  • This function is used to encode characters to UTF-8 format, why would you want to use it with moment? Maybe use moment('2015-07-13T22:00:00Z').format('format-needed'); – justMe May 07 '18 at 13:23
  • Possible duplicate of [Encode URL in JavaScript?](https://stackoverflow.com/questions/332872/encode-url-in-javascript) – Thomas Darvik May 07 '18 at 13:25

2 Answers2

0

It encodes the URI of the component so that it can be accessed easily.

For example -

https://www.google.com?myquery=my query

becomes

https%3A%2F%2Fwww.google.com%3Fmyquery%3Dmy%20query

after using encodeURIComponent()

But if you need to change the format of your date, you should use .format() method.

0

It could make some sense using JS encodeURIComponent() function on an ISO 8601 date only if you need to use that value as an URL parameter, passing it 'as-is' for example to a REST endpoint...

UPDATE 1:

You could use it, for example, this way:

var input = '2015-07-13T22:00:00Z';
var output = encodeURIComponent(input);
var endpoint = 'https://example.com/endpoint/date=' + output;

UPDATE 2:*

Or, this way:

var input = moment().toISOString();
var output = encodeURIComponent(input);
var endpoint = 'https://example.com/endpoint/date=' + output;
...
MarcoS
  • 17,323
  • 24
  • 96
  • 174