Date DatePicker - Processing

3

Gentlemen:

The date I have in the datepicker is in the dd/mm/yy format, however, the date in the database is yy-mm-dd (date field). How do I get the datepicker to display the default date BR and when it comes via POST, enter as EN format?

    
asked by anonymous 02.07.2015 / 23:03

3 answers

1

If you want to deal with shipping :

var dataQuebrada = $("#datepicker").val().split("/");
var novaData = new Date("20" + dataQuebrada[2], dataQuebrada[1] - 1, dataQuebrada[0]); 
// new Date(year, month, day);
// -1 no mês porque ele começa do 0
// "20", pois você só tem os 2 últimos digitos

If you want to handle receiving :

var $datepicker = $("#datepicker");
var dataQuebrada = $("#datepicker").val().split("-");
var novaData = new Date("20" + dataQuebrada[0], dataQuebrada[1] - 1, dataQuebrada[2]); 
$datepicker.datepicker();
$datepicker.datepicker('setDate', novaData);
    
02.07.2015 / 23:42
1

I usually treat this data on the server side, and using PHP I do this:

<input type="text" name="minha_data" 
 id="minha_data" class="form-control data datepicker" 
 value="<?php data_us_to_br($geral->dt_atendimento); ?>"    
 placeholder="dd/mm/aaaa" maxlength="10" minlength="10" />

and on the server side, when I get the POST like this:

$valorMinhaData = data_br_to_us($_POST['dt_verificacao']);

and the functions I am using in case are these:

function data_us_to_br($dateUSA)
{
    if($dateUSA) {
         $ano = substr($dateUSA, 0, 4);
         $mes = substr($dateUSA, 5, 2);
         $dia = substr($dateUSA, 8, 2);
         $dateBR = $dia .'/'. $mes .'/'. $ano;

         return $dateBR;
    } 

    return NULL;
}


function data_br_to_us($dateBR)
{
    if($dateBR) {
         $ano = substr($dateBR, 6, 4);
         $mes = substr($dateBR, 3, 2);
         $dia = substr($dateBR, 0, 2);
         $dateUSA = $ano .'-'. $mes .'-'. $dia;

         return $dateUSA;
    } 

    return NULL;
}
    
03.07.2015 / 19:12
0

From doing so with a single command line:

var d = $.datepicker.formatDate( "yy-mm-dd", $('#calendario').datepicker('getDate') );

Where the variable d is in the mysql format

And the $ ('# calendar') datepicker ('getDate')

  

link

    
03.07.2015 / 19:12