Datetime DD/MM/YYYY HH:mm and datepicker

Datetime DD/MM/YYYY HH:mm and datepicker

sdroulerssdroulers Posts: 43Questions: 14Answers: 0
edited September 2017 in General

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

  • allanallan Posts: 63,493Questions: 1Answers: 10,470 Site admin

    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 uses g:i A - i.e. it has AM or PM at the end, while the Moment format doesn't have that. Try adding A to the end of your moment format string.

    Allan

  • sdroulerssdroulers Posts: 43Questions: 14Answers: 0

    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

  • allanallan Posts: 63,493Questions: 1Answers: 10,470 Site admin
    Answer ✓

    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

  • Koen VerhoevenKoen Verhoeven Posts: 25Questions: 7Answers: 0
    One thing would be great : in this page example https://editor.datatables.net/examples/dates/formatting.html the server script tab is empty.
    

    I just saw that this page still does not show the serverside code. This would be really helpful for me!

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    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

This discussion has been closed.