Convert server time (UTC) to showing locale time (Danish)
Convert server time (UTC) to showing locale time (Danish)
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
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?
That hour is very likely to be due to the timezone's daylight saving adjustment - this Moment doc should help,
Colin
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.
Picked up form: https://datatables.net/forums/discussion/34759/datetime-recorded-in-utc-on-server-but-to-be-edited-in-local-time