Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Datetime to Unix timestamp in TypeScript

🎈 🎈 🎈
+1

    const date: Date = new Date('2018-06-15T12:10:45'); // Create Date object
    const unixTimestamp: number = Math.floor(date.getTime() / 1000); // Convert to seconds
    
    console.log(`Unix Timestamp: ${unixTimestamp}`);            
        

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

Code Explanation

In this TypeScript example, a Date object is created for '2018-06-15 12:10:45' using the new Date() constructor.
The getTime() method returns the number of milliseconds since the Unix epoch, and Math.floor() is used to round the result down to a whole number after dividing by 1000 to convert it to seconds.
The result is printed using console.log.



Other useful TypeScript date functions (JavaScript Date 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