Convert date string without punctuation in Date Javascript format

Asked

Viewed 326 times

2

I am wanting to format a date string without punctuation in a field with date format, for example:

sdata = '201909091504'

datacorreta = '2019-09-09 15:04' // resultado esperado

I tried with the .parse but it returned me a null value. What could I use to convert in this punctuation format?

  • The format will always be the same, year-month-day hour:minutes?

  • sdata.match(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/) then just concatenate

3 answers

2

Natively, the Date Javascript does not give many options to convert strings to a date (actually, few formats are in fact "official" and many others work differently depending on the browser).

The way is to manipulate the string manually. One way to do it is to:

let sdata = '201909091504';
let [ano, mes, dia, hora, minuto] = sdata.match(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/).slice(1, 6);
let dataFormatada = `${ano}-${mes}-${dia} ${hora}:${minuto}`;
console.log(dataFormatada); // 2019-09-09 15:04

The regular expression used in the method match uses the shortcut \d (which corresponds to digits from 0 to 9) and uses quantifiers {n} to pick up exactly n digits. I also use parentheses, which form capture groups, so each stretch in question is returned separately.

Then I use the method slice to take the stretch of the array that interests me (since match returns an array with various other information), attribute to the respective variables (using the syntax of destructuring assingment) and I grant everything.


One detail is that the above code does not check whether the date is valid (if the day is longer than 31, etc). If you want to do this, one way is:

function pad(valor) {
    return valor.toString().padStart(2, '0');
}

let sdata = '201909091504';
let [ano, mes, dia, hora, minuto] = sdata.match(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/).slice(1, 6).map(s => parseInt(s));
let d = new Date(ano, mes - 1, dia, hora, minuto);
if (d.getFullYear() === ano && d.getMonth() + 1 === mes && d.getDate() === dia
    && d.getHours() === hora && d.getMinutes() === minuto) {
    let dataFormatada = `${ano}-${pad(mes)}-${pad(dia)} ${pad(hora)}:${pad(minuto)}`;
    console.log(dataFormatada); // 2019-09-09 15:04
} else {
    console.log('data inválida');
}

I create a Date using the values obtained by regex (converting them to numbers with parseInt). Then I check if the values of the Date are the same as the original variables. I did this because the Date accepts values as day 32 and makes some adjustments (January 32 is adjusted to February 1, for example). So if any of the values is different, it is because the original date contains invalid values.

Also note that I subtracted 1 from the month because in Date javascript months are indexed to zero (January is zero, February is 1, etc).


Another alternative is to use the Moment js.:

let sdata = '201909091504';
let data = moment(sdata, 'YYYYMMDDHHmm'); // lê o formato acima e cria a data
// mostra a data em outro formato
console.log(data.format('YYYY-MM-DD HH:mm')); // 2019-09-09 15:04
<script src="https://momentjs.com/downloads/moment.min.js"></script>

One advantage of Moment.js is that it also validates the date. Just use isValid(), as shown in reply of the Virgilio.

  • 1

    Wow, the regex is even simple, but you have mythed about using it in combination with the Slice in just one line. Claps.

  • 2

    You really need the methods fromFormat and format in the Date...

  • @Leonardogetulio I even tried to see if it had like match return only what I need, but in the end I found it easier to use the slice even :-)

  • 1

    @Andersoncarloswoss There’s a lot of need for a decent native date API :-)

  • Looking a little more I found the lib date-fns, which could be a solution to Moment, since to use it we have to download the entire lib in the project and this is a bit of a joke, but I did not find any example using this format I described in the post. If someone knows how to use this lib could help us.

  • @Sergiorbj I never used the date-fns, but from what I saw, just use the formats indicated in the documentation (see the letters corresponding to the year, month, day, etc and mount the desired format). To turn the string into a date, use the parse, and to convert to the new format, use format

Show 1 more comment

1


You will need to manually format because javascript does not accept custom formatting of inputs/date entries. As long as you always work in the same format, you can use the function below:

sdata = '201909091504'
//datacorreta = '2019-09-09 15:05' //

function dataAPartirDeNumero(dataNumero) {
  dataFormatada = dataNumero.slice(0,4) +
    '-' + dataNumero.slice(4,6) +
    '-' + dataNumero.slice(6,8) +
    ' ' + dataNumero.slice(8,10) +
    ':' + dataNumero.slice(10,12);
   return dataFormatada;
}

dataFormatada = dataAPartirDeNumero(sdata);
console.log('data: '+ dataFormatada); //2019-09-09 15:04

  • It worked perfectly Leonardo, that’s what I needed! I thought there was a method ready for this hehe

1

Well, there is also a guy who can format this for you and also check if this really is a valid date with the library Moment.js, example:

sdata = '201909091504'
var result = moment(sdata, 'YYYYMMDDHHmm');
if (result.isValid())
{
   var htmlDiv = document.getElementById("new");
   htmlDiv.innerHTML = result.format('YYYY-MM-DD HH:mm');   
}
else 
{
   alert('date invalid');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment-with-locales.min.js"></script>

<div>
  201909091504
</div>  
<div id="new">
  
</div>

  • 1

    +1 for remembering the isValid (that I just forgot to mention in my reply) :-)

Browser other questions tagged

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