Convert server time (UTC) to showing locale time (Danish)

Convert server time (UTC) to showing locale time (Danish)

cha59cha59 Posts: 87Questions: 23Answers: 0
edited December 2021 in DataTables 1.10

I use a MySql timestamp for last update of my database. This is in UTC time, and I want to change it to danish time for display only. So I have tried to change this code a bit, but I cannot figure out how to show danish time.

// Date renderer for DataTables from cdn.datatables.net/plug-ins/1.10.21/dataRender/datetime.js
$.fn.dataTable.render.moment = function ( from, to, locale ) {
    // Argument shifting
    if ( arguments.length === 1 ) {
        locale = 'en';// want this to be 'da' (danish), not english ('en') 
        to = 'DD.MM.YYYY hh:mm';
        from = 'YYYY-MM-DD hh:mm:ss';
    }
    else if ( arguments.length === 2 ) {
        locale = 'en';// want this to be 'da' (danish) 
    }
 
    return function ( d, type, row ) {
        if (! d) {
            return type === 'sort' || type === 'type' ? 0 : d;
        }
 
        var m = window.moment( d, from, locale, true );
 
        // Order and type get a number value from Moment, everything else
        // sees the rendered value
        return m.format( type === 'sort' || type === 'type' ? 'x' : to );
    };
};

The page loads this (among others):

<script type="text/javascript" src="https://cdn.datatables.net/datetime/1.1.1/js/dataTables.dateTime.min.js"></script>

and the display of datetime looks like this:

{ data: "hold.timestamp", render: $.fn.dataTable.render.moment( 'DD.MM.YYYY hh:mm' )},

As of now, it displays according to my wishes ( 'DD.MM.YYYY hh:mm' ) but an hour wrong.
I'm pretty sure it's a small thing, but I can't figure it out. Please help.
Best regards
Claus

Answers

  • cha59cha59 Posts: 87Questions: 23Answers: 0

    I think, I might have misunderstood what "locale" means in my question above. Is the code just formatting a date according to national tradition, og is it also converting a UTC timestamp to local timezone?

  • colincolin Posts: 15,118Questions: 1Answers: 2,583

    That hour is very likely to be due to the timezone's daylight saving adjustment - this Moment doc should help,

    Colin

  • cha59cha59 Posts: 87Questions: 23Answers: 0

    PHP did the trick of setting danish time of a UTC timestamp from the database as well as formatting a datetime like d-m-Y H:i: . Couldn't make any other things work.

    $user_tz = ‘Europe/Copenhagen’;
    
    Field::inst( 'hold.timestamp' )
                ->validator( 'Validate::dateFormat', array(
                        'empty' => false,
                        'format' => 'Y-m-d H:i' ))                
                            ->getFormatter( function ( $val, $data, $opts ) use ( $user_tz ) {
                                                                $converted_date = new DateTime($val, new DateTimeZone( 'UTC' ));
                                                                $converted_date->setTimeZone( new DateTimeZone( $user_tz ));
                                                                return $converted_date->format('d-m-Y H:i');
                                                            } ),
    

    Picked up form: https://datatables.net/forums/discussion/34759/datetime-recorded-in-utc-on-server-but-to-be-edited-in-local-time

Sign In or Register to comment.