How to format datetime retrieved from database using Editor

How to format datetime retrieved from database using Editor

YanosYanos Posts: 6Questions: 2Answers: 1
edited January 2018 in Free community support

HI Everyone,

I have a field in a table of my datatabase that keeps track of the time the record is saved (it is a timestamp saved in this format: YYYY-MM-DD HH:mm:ss). That field is not in my form.

But I would like to display its values in my table in a specific format (DD-MM-YYYY HH:mm:ss). How can I go about it?

NB: I am using Editor
Thanks for your help.

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin
    Answer ✓

    Using a get formatter (assuming you are using the PHP libraries, docs are available for the .NET and Node libraries as well).

    You'd need to include the column as a field in the server-side code, but simply don't include it in the client-side Editor fields array.

    Allan

  • YanosYanos Posts: 6Questions: 2Answers: 1

    Thanks Allan, it really helped.

    Now my issue is that I can only have the date in English format.

    How can I have it in French format (DD-MM-YYYY) ?

    Thanks

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    A get formatter should be able to show that format. Could you show me the formatter you are using please?

    Allan

  • YanosYanos Posts: 6Questions: 2Answers: 1
    edited January 2018

    This is what I'm currently using

    **Field::inst( 'student.date_updated' )
    ->getFormatter( 'Format::date_sql_to_format', Format::DATE_ISO_850 )**
    This one gives me this results : 2018-01-24 09:51:54

    But the desired result is 24-01-2018 09:51:54
    When I tried this, it doesn't work,
    _**Field::inst( 'student.date_updated' )
    ->getFormatter( Format::date_sql_to_format( 'd-m-Y' ) )**_

    Kindly assist please,

    Thanks

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    Try:

    Field::inst( 'student.date_updated' )
      ->getFormatter( Format::datetime( 'd-m-Y H:i:s' ) )
    

    Allan

  • YanosYanos Posts: 6Questions: 2Answers: 1
    edited January 2018

    I've tried it but not working, I have this error message
    localhost says: DataTables warning: table id=example - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1

  • YanosYanos Posts: 6Questions: 2Answers: 1
    edited January 2018

    Hi Allan,

    I just found the solution,

    Here is the getformatter I finally used:

    ->getFormatter( 'Format::datetime', array( 'from' => 'Y-m-d H:i:s', 'to' => 'd/m/Y H:i' ) )

  • YanosYanos Posts: 6Questions: 2Answers: 1
    edited January 2018 Answer ✓

    I finally found the solution. This is the get formatter I finally used:

    ->getFormatter( 'Format::datetime', array(
    'from' => 'Y-m-d H:i:s',
    'to' => 'd/m/Y H:i'
    ) )
    
  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    Thanks - good to hear you have it working now. The datetime function I suggested above requires 1.7.0 or newer. With an older version of the library it would indeed throw a JSON error.

    Allan

This discussion has been closed.