Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-04-26
Convert Unix Timestamp to Datetime in QML

How to convert Unix timestamp to Datetime in QML

🎈 🎈 🎈
+1

    import QtQuick 2.0

    Rectangle {
        width: 200
        height: 100
    
        property int unixTimestamp: 1789818215
    
        Text {
            text: {
                var date = new Date(unixTimestamp * 1000); // Multiply by 1000 to convert to milliseconds
                var year = date.getFullYear();
                var month = ("0" + (date.getMonth() + 1)).slice(-2);
                var day = ("0" + date.getDate()).slice(-2);
                var hours = ("0" + date.getHours()).slice(-2);
                var minutes = ("0" + date.getMinutes()).slice(-2);
                var seconds = ("0" + date.getSeconds()).slice(-2);
                return "Converted Date: " + year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
            }
            anchors.centerIn: parent
        }
    }         
        

Output:
Converted Date: 2026-09-19 11:43:35
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this QML example, the Unix timestamp 1789818215 is multiplied by 1000 to convert it from seconds to milliseconds, as QML's Date object expects milliseconds. The toLocaleString() method converts the timestamp into a readable string, which is displayed in the Text element.
The result will display a formatted date such as 2026-09-19 11:43:35.



Other useful QML date functions (JavaScript-like API)

Function Description Example
new Date() Creates a new date object representing a specific date and time or the current date and time if no argument is provided. new Date(1514764800 * 1000) returns Mon Jan 01 2018 00:00:00 GMT+0000
getTime() Returns the number of milliseconds since January 1, 1970 for the given date. date.getTime() returns 1531657845000 for 2018-07-15 12:30:45
toLocaleString() Returns a string with a language-sensitive representation of the date. date.toLocaleString() returns 1/1/2018, 12:00:00 AM
toISOString() Converts a date object to an ISO 8601 string in UTC. date.toISOString() returns 2018-01-01T00:00:00.000Z
toUTCString() Converts a date to a string in UTC format. date.toUTCString() returns Mon, 01 Jan 2018 00:00:00 GMT
setFullYear() Sets the year for a date object. date.setFullYear(2020) sets the year to 2020.
getFullYear() Gets the year from a date object. date.getFullYear() returns 2018.
setMonth() Sets the month for a date object (0 for January). date.setMonth(0) sets the month to January.
getMonth() Gets the month from a date object (0 for January). date.getMonth() returns 0 for January.
getDate() Gets the day of the month from a date object. date.getDate() returns 1.
setHours() Sets the hours for a date object. date.setHours(12) sets the hours to 12.
getHours() Gets the hours from a date object. date.getHours() returns 0 (for midnight).
Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-04-26
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder