Search code examples
samsung-smart-tvsmart-tvsmart-tv-alliance

Samsung SmartTV get current time


How can I get current time for Samsung SmartTV app written on HTML, CSS and JavaScript?

The problem is in Javascript Date object. When I call new Date() on the web environment, I receive system Date value with current time. But when I instantiate date on SmartTV it returns incorrect result. I expected the same behavior as on the web. In TV settings time is set to be automatically determined.

Correct time returns only when I set time zone manually on first TV boot. I suppose it is bug of Samsung SmartTV platform.


Solution

  • I had the same problem. new Date() on Samsung Smart TVs doesn't get the time according to the current setting, but instead gets the underlying system time. It always returns a UTC timestamp, too.

    Fortunately, there's an API function, GetEpochTime(), that allows you to (sort of) fix it.

    Here's the code I'm using on Samsung platforms:

    (function (OldDate) {
        // Get a reference to Samsung's TIME API
        var time = document.createElement('object');
        time.setAttribute('classid', 'clsid:SAMSUNG-INFOLINK-TIME');
        document.body.appendChild(time);
    
        // Replace the existing Date() constructor
        window.Date = function (a, b, c, d, e, f, g) {
            switch (arguments.length) {
                case 0:
                    return new OldDate(time.GetEpochTime() * 1000);
    
                case 1: return new OldDate(a);
                case 2: return new OldDate(a, b);
                case 3: return new OldDate(a, b, c);
                case 4: return new OldDate(a, b, c, d);
                case 5: return new OldDate(a, b, c, d, e);
                case 6: return new OldDate(a, b, c, d, e, f);
                case 7: return new OldDate(a, b, c, d, e, f, g);
            }
        };
    
        // Copy static function properties
        Date.now   = function () { return time.GetEpochTime() * 1000; };
        Date.UTC   = OldDate.UTC;
        Date.parse = OldDate.parse;
    })(Date);
    

    new Date() now works fine for displaying the current time on my 2013 Samsung Smart TV. You might still have problems when comparing timestamps to the current time, however.