gpt4 book ai didi

coldfusion - ColdFusion 中的相对日期

转载 作者:行者123 更新时间:2023-12-02 22:30:25 24 4
gpt4 key购买 nike

寻找类似 this 的函数在 ColdFusion 中,允许我将日期显示为“10 分钟前”或“2 天前”或“一个月前”。

最佳答案

虽然与 udf 没有本质上的不同,但我喜欢 this guy's approach 。没有经过严格测试,但你也可以这样做:

编辑你没有提到版本,所以我假设CF8

<cffunction name="relativeDate" returnType="string" access="public" output="false">
<cfargument name="theDate" type="date">
<cfset var x = "" />
<cfset var diff = "" />
<cfset var result = "unknown" />
<cfset var dateNow = now() />
<cfset var codes = [ "yyyy", "m", "ww", "d", "h", "n", "s" ] />
<cfset var names = [ "year", "month", "week", "day", "hour", "minute", "second" ] />

<cfif dateCompare(arguments.theDate, now()) gt 0>
<!--- replace with other code to handle future dates ....--->
<cfthrow message="Future date handling not implemented">
</cfif>

<!--- check each date period ...--->
<cfloop from="1" to="#arrayLen(codes)#" index="x">
<cfset diff = abs( dateDiff(codes[x], arguments.theDate, dateNow) ) />
<!--- this is the greatest date period --->
<cfif diff gt 0 >
<cfif diff gt 1>
<cfset result = "about "& diff &" "& names[x] &"s ago" />
<cfelseif names[x] eq "hour">
<cfset result = "about an "& names[x] &" ago" />
<cfelse>
<cfset result = "about a "& names[x] &" ago" />
</cfif>

<cfbreak>
</cfif>
</cfloop>

<cfreturn result />
</cffunction>

关于coldfusion - ColdFusion 中的相对日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2679323/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com