You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ColdFusion中将UNIX时间转为可读时间且不进行时区转换?

Convert UNIX Timestamp to GMT/UTC Readable Time (No Timezone Conversion)

Got it, let's fix this for you! The problem with your original function is that dateConvert("utc2Local") forces a shift to your server's local timezone—exactly what you want to avoid. Since UNIX timestamps count seconds from 1970-01-01 00:00:00 UTC, we can skip that conversion step entirely to get the raw GMT/UTC time.

Here's the adjusted function that returns the exact GMT readable time you need:

<cffunction name="EpochToGMT" hint="Converts UNIX timestamp to GMT/UTC readable datetime">
    <cfargument name="dateSeconds" type="numeric" required="true" hint="UNIX timestamp (seconds since 1970-01-01 UTC)">
    
    <cfscript>
        // Base epoch start time (UTC)
        var startDate = createDateTime('1970','01','01','00','00','00');
        // Calculate UTC datetime by adding timestamp seconds to the base date
        var utcDateTime = dateAdd('s', arguments.dateSeconds, startDate);
        
        // Format to match your desired output: e.g., "Friday, 20 July 2018 8:20:00 PM"
        return dateFormat(utcDateTime, "dddd, dd mmmm yyyy") & " " & timeFormat(utcDateTime, "h:mm:ss TT");
    </cfscript>
</cffunction>

Breakdown of the changes:

  • We remove the dateConvert call entirely—dateAdd already gives us a UTC datetime value since we're starting from the UTC epoch.
  • We use dateFormat and timeFormat to structure the output exactly like your example. Alternatively, you can use a single datetimeFormat call for conciseness:
    return datetimeFormat(utcDateTime, "dddd, dd mmmm yyyy h:mm:ss TT");
    

Test with your example timestamp:

writeOutput(EpochToGMT(1532118000));
// Output: Friday, 20 July 2018 08:20:00 PM

This will give you the pure GMT time without any unwanted timezone shifts.

内容的提问来源于stack exchange,提问作者wilbi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:38:40