Convert maturity factor (number of days) of billet into date dd/mm/yyyy

Asked

Viewed 11,027 times

10

I have the following digit line:

74893.12004 21627.007186 37931.981056 7 60750000001400

As already seen in this question, I figured out how to calculate the salary mathematically:

Extract Thread Digit Maturity

Need the equivalent in JS, which is to extract the maturity factor (the first four characters of the last block, in the example above 6075), and add this number to the date 7/10/1997 (the date 7 October 1997 is always this, the basis of all the standard boletos Febraban), obtaining maturity in the format dd/mm/yyyy.

Example: for factor 1001 the resulting date has to be 4/07/2000.

  • 2

    you have already managed to extract only the field related to the maturity code?

  • @Bacco, the other question was for Delphi, how do I do it in java ?

  • @user7605 you want java or javascript?

  • @Erlon, I have already used the: test.substr(40,4);

  • @Erloncharles, JAVASCRIPT..

  • @Bacco posted there as comment the link of the other question

  • @user7605 I will try to save your question, if you don’t like the editing, you can reverse it later.

  • Okay @Bacco, thank you for your attention. Remembering that I need to convert this code that contains the MATURITY in the correct expiration date in the style xx/xx/xxxx ok ?

  • @Erloncharles, in Delphi would look like this: Result := Strtodate('07/10/1997') + Strtoint(Copy(Codigobarras, 41, 4))..

  • Great @Bacco, thanks!

  • @user7605 when asking next questions, try to be clear this way, and focus on the main problem, so help everyone (and we can help you better).

  • Good question, didn’t know it was possible :) +1

  • Thank you @Bacco....

  • See also this link: http://boletobancario-codigodebarras.blogspot.com.br/

Show 9 more comments

2 answers

7


See if this works for you:

var barra = "74893.12004 21627.007186 37931.981056 7 60750000001400";
var vencimento = barra.slice(40, 44); // captura 6075 
var date = new Date('10/07/1997');
date.setTime(date.getTime() + (vencimento * 24 * 60 * 60 * 1000));

alert(("0" + (date.getDate())).slice(-2) + '/' + ("0" + (date.getMonth() + 1)).slice(-2) + '/' + date.getFullYear());

Or by using the number factor 1001:

var barra = "74893.12004 21627.007186 37931.981056 7 60750000001400";
var vencimento = 1001;
var date = new Date('10/07/1997');
date.setTime(date.getTime() + (vencimento * 24 * 60 * 60 * 1000));

alert(("0" + (date.getDate())).slice(-2) + '/' + ("0" + (date.getMonth() + 1)).slice(-2) + '/' + date.getFullYear());

Jsfiddle

As well pointed out by @Bernardo Botelho, the above code will probably return the wrong day if we are on daylight saving time, a way to detect this in Javascript is described in that article(in English).

  • only have a problem, was perfect your code, more precise that it appears the 0 at maturity, for example, 26/07/2014, it appears 26/7/2014 the "7" without the "0" before, understood ? can we fix this ?

  • Show this brazilian stackoverflow huh...:)

  • @Sunstreaker, he still does not show the "0" ZEROS on maturity.

  • 1

    The problem with using hours is that some days do not have 24 hours due to daylight saving time. If an expiration date falls on that day, you will have the incorrect day.

  • I believe that adding about 5 hours in the formula would solve this problem. (or subtracting, I did not analyze if the reference is 0h00 or 24h00)

  • I understood..... I will do this test, more as I do not eliminate the zeros ? he’s showing like this 3/6/2014, the correct would be 03/06/2014.

  • @Sunstreaker, almost perfect, the problem that he’s adding another day on the clock...That’s it, the zeroes showed up. If the ticket won yesterday 28/05/2014, it is showing 29/05/2014.

  • @Sunstreaker, already solved, I just changed the date.getDate() + 1) to the date.getDate() + 0). Thank you.

  • The error happens on the next date of Brazilian daylight saving time: 18/10/2014. By adding up hours you fall at midnight on the 18th. As it is on this day that daylight saving time begins, the time is set to 17/10/2014 at 11:00 pm. All boletos that fall on the 18th will have the date changed to the 17th, incorrectly. See Jsfiddle: http://jsfiddle.net/bzxbot/mejDr/ . The issue can be noted in Chrome and Firefox.

Show 4 more comments

4

Adding a method to the Date class:

var linhaDigitavel = "74893.12004 21627.007186 37931.981056 7 60750000001400";

var dias = parseInt(linhaDigitavel.substr(40, 4));

Date.prototype.adicionarDias = function(dias) {
    var data = new Date(this.valueOf());
    data.setDate(data.getDate() + dias);
    return data;
};

// Meses são indexados em zero em JavaScript, logo é necessário subtrair 1 do mês desejado.
var dataInicialFebraban = new Date(1997, 10 - 1, 7);

alert(dataInicialFebraban);

alert(dataInicialFebraban.adicionarDias(dias));

// Resultado: 26/05/2014

Link to the Jsfiddle used: http://jsfiddle.net/bzxbot/YEaLR/

As a reference, I will leave a solution that NAY must be used:

var dias = 6075;
var inicio = new Date(1997, 10 - 1, 7);
var vencimento = new Date();
vencimento.setDate(today.getDate() + dias);
alert(vencimento);
// Resultado (incorreto): 24/12/2030

Jsfiddle from the incorrect example: http://jsfiddle.net/bzxbot/xTD6s/

Reference for the same question in Stack Overflow: https://stackoverflow.com/questions/563406/add-days-to-datetime

  • If you want, you can update your observation. I typed wrong in the question, the correct month is 7 and not 6 as I had put before, but already corrected the question ;) In fact the result has to be 04/07/2000, as obtained in your tests.

  • 1

    Okay, I removed the observation.

Browser other questions tagged

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