Playing with JavaScript String


Hello, I need a little advice on this, it turns out that I have this chain

var timestamp = 20160525083000

then I want to divide it and try and what I need is that in the end I have two variables left

var1= 25-05-2016


var2= 08:30

I do not know if I'm on the right track but I did something like this:

var oFecha = timestamp;
var fecha_f = oFecha(0,7);

var oHora = timestamp;
var Hora = timestamp(8,11);

But I do not know how to order it or if what I did is fine because I try to test it on the console and it says undefined every time I declare a variable: /

asked by Naoto Amari 14.03.2018 в 15:29

5 answers


I must clarify that since you have defined the variable timestamp this is not string, so to extract the substrings I have to convert it into the following code, however if it were timestamp = '20160525083000', we will get to line 2 where I convert to string the variable timestamp . Your string consists of 14 characters, the first 4 is the year of your date so substr (0,4) corresponds to this part, the next 2 are the month, which is represented by the substr (4,2), the Next 2 is the day, ie substr (6.2), and so on until the hour and minute part.

var timestamp = 20160525083000;
timestamp = timestamp.toString();
var fecha = timestamp.substr(6,2)+ "-" +
            timestamp.substr(4,2)+ "-" +

var hora = timestamp.substr(8,2) + ":" +
answered by 14.03.2018 / 15:45

I'll give you a way to do it using the date management library called Moments.js

var timestamp = 20160525083000;

var fecha = moment(timestamp, "YYYYMMDDhmmss");

var1 = fecha.format('DD-MM-YYYY');
var2 = fecha.format('h:mm');

<script src=""></script>
<script src=""></script>
answered by 14.03.2018 в 15:44

Hi, I need some advice on this, it turns out that I have this   string

var timestamp = 20160525083000

The value you are assigning to timestamp is not a string, it is a number. To be treated as a string you should do the assignment by placing the value in single or double quotes, as follows:

var timestamp = '20160525083000';

There are several ways to extract substrings and then use these to "arm" other strings. Here are two of them:

  • Use substr
  • var timestamp = '20160525083000';
    var var1 = timestamp.substr(0,4) + '-' + timestamp.substr(4,2) + '-' + timestamp.substr(6,2);;
    var var2 = timestamp.substr(8,2) + ':' + timestamp.substr(10,2);;
  • Use regular expressions
  • var timestamp = '20160525083000';
    var var1 = timestamp.replace(/(\d{4})(\d{2})(\d{2})(\d{6})/, '$3-$2-$1');;
    var var2 = timestamp.replace(/(\d{8})(\d{2})(\d{2})(\d{2})/, '$2:$3');;

    Regarding the error, this happens because JavaScript does not have a syntax like oFecha(valor1,valor2) for variables. That syntax could be used for functions, but that should be treated in another question.

    answered by 14.03.2018 в 17:16

    If the string timestamp will always have that format yyyymmddhhiiss you have a substring and store it in the variables you need.


    STRING.substr (Int home, Length);

    var timestamp = 20160525083000; //numerico
    timestamp = timestamp.toString(); // String
    var fecha_f = timestamp.substr(6,2) + "-" + timestamp.substr(4,2) + "-" + timestamp.substr(0,4);    
    var Hora = timestamp.substr(8,2)+":"+timestamp.substr(10,2);
    answered by 14.03.2018 в 15:52

    I have done it in the following way ... Creating an object with the number of characters that each part of the date has ... minutes has 2 , year has 4 , and so on with all the parts.

  • The first thing I do is convert the number to a string (string or text)

  • Then I create the object, all the parts have 2 characters minus the year, which has 4 .

  • I scroll through each part of the object, in the same order as I did the object, and I subtract a variable called posición . Always subtract 2 , except at the end, which subtracts 4 . This variable does not count the number of seconds, so it starts in -2 .

  • The object assigned the value of each part of the date, cutting the text in the selected part.

  • Finally, I call the function that formats the date, I declare the variables var1 and var2 and I show them.

    function formatear_fecha(timestamp)
      var texto=""+timestamp
      /* Objeto tiempo
        c es la cantidad de caracteres
        v es el valor
      var t = {
        horas  :{c:2},
        día    :{c:2},
        mes    :{c:2},
        año    :{c:4}
      var posición = -2
      for(var i in t)
        var siguiente = t[i].c
        posición -= t[i].c
      return [
    var timestamp = 20160525083000
    var fecha_formateada =  formatear_fecha(timestamp)
    var var1 = fecha_formateada[0]
    var var2 = fecha_formateada[1]
    console.log("var1 es "+var1)
    console.log("var2 es "+var2)
    answered by 14.03.2018 в 16:15