JSON.parse represents the date in the format

2017-01-09T14: 02: 25.207

There are many dates in the document; it is not optimal to apply the toLocaleDateString function to each. How to bring the format of all dates in the document? For example to this

09.01.2017

  • JSON.parse does not parse dates, usually. He leaves them with the string that comes from the server - Grundy
  • @Grundy enive, the format does not suit, but JS should have the means to fix it -
  • toLocaleDateString is one of the options for converting a date to the desired format. Since the date is returned from the server - if it is needed only for output - let the server return immediately in the right format. Now it is not clear how you get the data, apply parse to it, and how you want to use it later - Grundy

1 answer 1

If, as Grundy said, it is not possible to get the desired format from the server, then you can do this:

 var date = '2017-01-09T14:02:25.207'; document.querySelector('button').onclick = function() { var new_date = date.replace(/^(\d+)\-(\d+)\-(\d+)\D.+$/, '$3.$2.$1'); // Преобразовываем дату console.log(new_date); }; 
 <button>Показать дату</button> 

  • one
    And how is this method better toLocaleDateString? -
  • @ sergeitambovtsev, I said that he is better? - Yuri
  • @ sergeytambovtsev, for example, that you do not need to give a string in the date. while toLocaleDateString is a date method, not a string - Grundy