Datetime DD/MM/YYYY HH:mm and datepicker
Datetime DD/MM/YYYY HH:mm and datepicker
Hi,
I'm dealing with a problem which is driving me nuts !
I used the example on this page to create a datetime datepicker and it runs successfully.
https://editor.datatables.net/examples/dates/datetime.html
However, I wanted to adapt it to a specific format : "DD/MM/YYYY HH:mm" --> "12/09/2017 17:00" but I'm strugling with it !
My input data comes from a MySql base at this format 2017-09-12 17:00:00
Here is the JS code for the field:
{
label: 'Date et heure de comptage:',
name: 'counting_datetime',
type: 'datetime',
def: function () { return new Date(); },
format: "DD/MM/YYYY HH:mm",
opts: {
showWeekNumber: true
}
}
And the php:
Field::inst( 'counting_datetime' )
->validator( 'Validate::notEmpty' )
->validator( 'Validate::dateFormat', array(
"format" => "d/m/Y g:i A",
"message" => "La date doit être au format jj/mm/aaaa"))
->getFormatter( 'Format::datetime', array(
'from' => 'd/m/Y H:i:s',
'to' => 'd/m/Y g:i A'
) )
->getFormatter( 'Format::date_sql_to_format', 'd/m/Y H:i:s' )
->setFormatter( 'Format::datetime', array(
'from' => 'd/m/Y g:i A',
'to' => 'd/m/Y H:i:s'
) )
Many thanks for your help!
Sébastien
This question has an accepted answers - jump to answer
Answers
Is the formatted date showing up correctly in the DataTable?
The
format
for Moment doesn't appear to match the format used for the output - specifically your get formatter usesg:i A
- i.e. it hasAM
orPM
at the end, while the Moment format doesn't have that. Try addingA
to the end of your moment format string.Allan
Thanks very much,
I finally made it working with the following code (the A option wasn't fundamental to me):
JS:
{
label: 'Date et heure de comptage:',
name: 'counting_datetime',
type: 'datetime',
def: function () { return new Date(); },
format: 'DD/MM/YYYY HH:mm',
opts: {
showWeekNumber: true
}
// fieldInfo: 'Euro style date with 24 hour clock'
}
PHP:
Field::inst( 'counting_datetime' )
->validator( 'Validate::dateFormat', array(
'format' => 'd/m/Y H:i'
) )
->getFormatter( 'Format::datetime', array(
'from' => 'Y-m-d H:i:s',
'to' => 'd/m/Y H:i'
) )
->setFormatter( 'Format::datetime', array(
'from' => 'd/m/Y H:i',
'to' => 'Y-m-d H:i:s'
) )
One thing would be great : in this page example https://editor.datatables.net/examples/dates/formatting.html the server script tab is empty.
I think this php code could be of great help to understand better the correspondance between the js and php date formats (in complement of this link)
Again, many thanks!
Sébastien
Thanks for pointing that out - I'm not sure why that is the case there! The PHP script is in the Editor PHP download package if you want to see if before I resolve that one.
Allan
I just saw that this page still does not show the serverside code. This would be really helpful for me!
Good spot, thanks for reporting that (we missed it the first time, apologies). I've raised it internally (DD-1397 for my reference) and we'll report back here when there's an update.
Cheers,
Colin