Format date coming from SQL SERVER Database

Asked

Viewed 645 times

0

The date that comes from SQL SERVER comes in the format like this: 20181212. I tried to format, but it only works if it comes in 2018-12-12 format.

I place this date inside a table by API I pull where ta data (date comes from API).

function Progress() {

        fetch('http://API_AQUI', {
            method: 'POST',
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            body: `L2_PRODUTO=${id}`
        }).then(response => response.json().then(data => ({
            data: data,
            status: response.status
        })
        ).then(res => {
            res.data.map(element => {
                $('.progresso-1').append(`${element.DATAUM}`);
                 $('.progresso-2').append(`${element.DATADOIS}`);
            }

There on the front I put:

<tr>
    <td class="td-b">Data Um</td>
    <td class="progresso-1"></td>
</tr>

<tr>
    <td class="td-b">Data Dois</td>
    <td class="progresso-2"></td>
</tr>

Only it only comes in the format it gives in the SQL SERVER dataset: 20181212.

I got this shape ready:

                    function formatarData(str){
                    return [str.slice(0, 4), str.slice(4, 6), str.slice(-2)].join('/'); // para formato yyyy-mm-dd
                    // ou para retornar um objeto Date
                    return new Date(str.slice(0, 4), Number(str.slice(4, 6)) + 1, str.slice(-2));

                  }
                  function formatDate(str)
                  {
                      return str.split('/').reverse().join('/');
                  }  

But it keeps coming 2018/12/12

3 answers

0

I still can’t comment. Is the date that goes to the database entered or comes by default? You already tried to use php

$date = date('d-m-Y', strtotime( $row['date'] ))

This is an example if you fetch the date from a database field

But you can always use SQL code for conversion

CONVERT(data_type(length), expression, style)

Take a look at this link

CONVERT SQL

0


You can break it with the method substr, that takes part of a string using a parameter that says where to start and another parameter that tells you how many characters to pick up from the initial (if the amount is not reported, it will pick up all from the informed Indice):

function formatDate( date ){
    if( typeof date === "number" ) date = date.toString();

    //Posição 0, 4 digitos = ano
    //Posição 4, 2 digitos = mes
    //Posição 6, 2 digitos = dia
    return `${date.substr( 6 )}-${date.substr( 4, 2 )}-${date.substr( 0, 4 )}`;
}

It’ll be like this :

$( '.progresso-1' ).append( `${formatDate(element.DATAUM)}` );
$( '.progresso-2' ).append( `${formatDate(element.DATADOIS)}` );
  • Stay 2018-01-23 and not dd-mm-yyyy

  • Just change places, it was just an example, I’ll change you to see

0

By default, the date is stored in the Mssql or mysql database in yyyy-mm-dd format, for example today: 2019-11-07.

So I just use this very simple function:

function dataBDFormatar(date){
	var strData = date.substr(8, 2)+"/"+date.substr(5, 2)+"/"+date.substr(0, 4);
    return strData;
}

The Return will be 06/11/2019

Browser other questions tagged

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