Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Convert Datetime to Unix Timestamp in QML

How to convert Datetime to Unix timestamp in QML

🎈 🎈 🎈
+1

    import QtQuick 2.0

    Rectangle {
        width: 200
        height: 100
    
        property string dateStr: "2021-03-18T09:17:43" // Date string
    
        Text {
            text: {
                var date = new Date(dateStr); // Create a Date object
                var unixTimestamp = Math.floor(date.getTime() / 1000); // Convert to seconds
                return "Unix Timestamp: " + unixTimestamp;
            }
            anchors.centerIn: parent
        }
    }            
        

Output:
Unix Timestamp: 1616059063
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this QML example, a Date object is created from a string representation of the date (2021-03-18T09:17:43).
The getTime() method returns the time in milliseconds since January 1, 1970, which is then divided by 1000 to convert it to seconds. The result is displayed as a Unix timestamp.



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-08-20
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder