Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Datetime to Unix timestamp in ColdFusion

🎈 🎈 🎈
+1

    <cfset dateTime = CreateDateTime(2022, 5, 15, 8, 30, 45)>

    <!-- Convert date to Unix timestamp -->
    <cfset unixTimestamp = DateDiff("s", CreateDateTime(1970, 1, 1, 0, 0, 0), dateTime)>
    
    <cfoutput>
        Unix Timestamp: #unixTimestamp#
    </cfoutput>
        

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

Code Explanation

To convert a human-readable date to a Unix timestamp in ColdFusion, the DateDiff() function is used. This function calculates the difference in seconds between two dates.
The Unix timestamp is the number of seconds between a given date and the Unix epoch (January 1, 1970). In the example, the DateDiff("s", CreateDateTime(1970, 1, 1, 0, 0, 0), dateTime) function calculates the difference between '2022-05-15 08:10:45' and '1970-01-01 00:00:00', resulting in the Unix timestamp 1652602245.
This value is then output using the cfoutput tag.



Other useful ColdFusion date functions

Function Description Example
CreateDateTime() Creates a datetime object from the specified year, month, day, hour, minute, and second. CreateDateTime(2022, 5, 15, 8, 30, 45) returns May 15, 2022 08:10:45
DateAdd() Adds a specified time interval (e.g., days, hours, seconds) to a date or datetime object. DateAdd("d", 5, now()) adds 5 days to the current date.
DateDiff() Returns the difference between two dates in the specified interval (e.g., days, hours, seconds). DateDiff("s", CreateDateTime(1970, 1, 1, 0, 0, 0), now()) returns the difference in seconds since the Unix epoch.
Now() Returns the current date and time. Now() returns 2024-08-28 12:34:56
Day() Returns the day of the month from a specified date. Day(Now()) returns 28 if the current day is the 28th.
Month() Returns the month from a specified date. Month(Now()) returns 8 for August.
Year() Returns the year from a specified date. Year(Now()) returns 2024.
TimeFormat() Formats a time as a string according to the given mask. TimeFormat(Now(), "HH:mm:ss") returns 12:34:56.
DateFormat() Formats a date as a string according to the given mask. DateFormat(Now(), "yyyy-mm-dd") returns 2024-08-28.
GetDayOfWeek() Returns the day of the week as a number (1=Sunday, 7=Saturday). GetDayOfWeek(Now()) returns 4 for Wednesday.
GetTimeZoneInfo() Returns information about the current time zone, including the name and offset from UTC. GetTimeZoneInfo() returns {isDST=true, rawOffset=-300, timezone='America/New_York'}.
ParseDateTime() Converts a string to a datetime object based on the date and time format. ParseDateTime("2022-05-15 08:10:45") returns May 15, 2022 08:10:45.
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