Tell me when there is a line of 10 characters how to validate it regularly in the format dd.mm.yyyy and cut it into 2 characters. For example:

var data = '1459839599' // два последних символа нужно обрезать тут, изначально их тут 10 , а надо что б в output было 8 var reg = /\d{2}\.{2}\.{4}/; // я так понимаю добавление точки я тут не правильно сделал? var output = data.replace(reg); console.log(output); // 14.59.8395 хотелось бы получить 

  • 2
    In fact, your 10-digit number is the number of seconds from the starting point of the time, and the date for it will not be 14.59.8395, but 04/04/2016. - Visman
  • As if you were right, but how to set the necessary data format? - user3319778
  • Your question has already been answered correctly. - Visman

2 answers 2

 var data = '1459839599'; var reg = /(\d{2})(\d{2})(\d{4})(\d{0,})/; var output = data.replace(reg, '$1.$2.$3'); console.log(output); // 14.59.8395 

    Note: Although the author accepted the answer @greybutton, I will provide a code based on my comment:

    In fact, your 10-digit number is the number of seconds from the starting point of the time, and the date for it will not be 14.59.8395, but 04/04/2016.

     function formatDate(date) { var dd = date.getDate(); if (dd < 10) dd = '0' + dd; var mm = date.getMonth() + 1; if (mm < 10) mm = '0' + mm; var yy = date.getFullYear(); return dd + '.' + mm + '.' + yy; } var data = '1459839599'; var tmp = new Date(data * 1000); console.log(formatDate(tmp)); 

    Tutorial https://learn.javascript.ru/datetime

    • You can simply use the toString () method instead of formatDate (), so as not to detract from the essence of the solution. - jfs