Field returning date in Brazilian standard and the other in American in typescript. How to validate?

Asked

Viewed 79 times

1

I have a field validation problem, in which when the date returns from my API in American format is made the conversion to Brazilian as below:

formatDate(date: string) {
if (!date) {
  return '-'
}
const [year, month, day] = date.split(/[^\w\s]/gi);
return `${day}/${month}/${year}`;   

}

But when my service returns the date in Brazilian format it changes to the American standard rsrsrsrs.... and it was to be in Brazilian format.

Does anyone have any suggestion how to validate this so that when comes Brazilian standard, continue Brazilian?

  • It seems to me more a problem of its service than of angular or algorithm. It would be better if it sent something like {day:xx, Month:xx, year:xxxx}

1 answer

0

From to do separate functions like this:

custom.convertDatePortuguestoSql=function(date){
    if(date != null && date != ""){
        var s = date.split("/");
        var ano = s[2];
        var mes = s[1];
        var dia = s[0];
        return ano+"-"+mes+"-"+dia;
    }
    else return '';
};  

custom.convertDateSqltoPortugues=function(date){
    if(date != null){
        var s = date.split("-");
        var ano = s[0];
        var mes = s[1];
        var dia = s[2];
        return dia+"/"+mes+"/"+ano;
    }else{
        return '';
    }
};

Ai you call each whatever your need, but it really seems to me that your problem is not in the function itself but in the way the rest of your code is working, ai ai teria que nos mostrar mais códigos para analisar.

Browser other questions tagged

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