Created
October 21, 2017 23:03
-
-
Save paulojp-dev/8ef7e0f92e161d59e51577e49875ff12 to your computer and use it in GitHub Desktop.
JavaScript - Utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// FORMAS DIFERENTES DE RECEBER UMA DATA | |
//new Date(this._inputData.value.split('-')), //ou | |
// troca todas as ocorrências de '-' por ',' | |
new Date( | |
this._inputData.value.split(/-/g, ',') | |
); | |
// o '...' siginifica que o método vai receber o array como parâmetros separados | |
new Date(... | |
this._inputData.value | |
.split('-') | |
.map(function(item, indice) { // permite percorrer todos os items do array, podendo modificá-los | |
return item - indice % 2; // neste caso só vai subtrair quando o indíce for '1', já que ele vai de 0-2 | |
// pois os meses quando recebidos 'int', são contados 0-11 | |
}) | |
); | |
static textoParaData(texto) { | |
// obriga o padrão: 0000-00-00 | |
// '^' começa com ; '$' termina com | |
if (!/^\d{4}-\d{2}-\d{2}$/.test(texto)) | |
throw new Error('Deve estar no formato yyyy-mm-dd'); | |
return new Date(...texto.split('-').map((item, indice) => item - indice % 2)); | |
} | |
static dataParaTexto(data) { | |
return `${data.getDate()}/${data.getMonth() + 1}/${data.getFullYear()}`; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment