如何在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
dateConvertcall entirely—dateAddalready gives us a UTC datetime value since we're starting from the UTC epoch. - We use
dateFormatandtimeFormatto structure the output exactly like your example. Alternatively, you can use a singledatetimeFormatcall 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
相关产品推荐
相关产品推荐

