TimeFormat()会将日期更改为您当地的时区吗?

时间:2014-06-20 18:02:13

标签: datetime coldfusion time-format

时间格式会改变到当地时区的时间吗?

我正在使用应用程序上的时区。用户在我们的数据库中设置他们的时区:( - 2,-3,-4等)。然后我得到一个我需要为用户显示的日期,添加/减去他们的时区值:

<cfset LOCAL.newDate = DateAdd("h",LOCAL.gmtOffset,LOCAL.utc_ts)>

然后设置变量以显示给他们:

<cfset LOCAL.dateToDisplay = dateformat(LOCAL.newDate,"mm/dd/yyyy") & ' - ' & timeformat(LOCAL.newDate,"hh:mm tt")>

当我查看LOCAL.newDate时,它等于:{ts '2013-07-08 08:57:23'}

当我查看LOCAL.dateToDisplay时,它等于:07/08/2013 - 03:57 AM

为什么时间格式会改变时间的价值?有什么想法吗?

谢谢!

编辑:添加了全部功能

<cffunction name="handleDate" access="remote" output="false" returnformat="plain" returntype="String" hint="Handle timezones and date formatting">
        <cfargument name="origTS" required="false" type="any" default="" />
        <cfargument name="displayTime" required="false" type="boolean" default=1 />

        <!--- Parse Date --->
        <cfset LOCAL.parsed_date = CreateODBCDateTime(arguments.origTS)>

        <!--- Convert date to UTC --->
        <cfset LOCAL.utc_ts = DateConvert("local2utc", LOCAL.parsed_date)>

        <!--- If daylight savings time is on, add an hour to users gmt offset --->
        <cfif GetTimeZoneInfo().isDSTon>
            <cfset LOCAL.gmtOffset = session.gmtOffset + 1>
        <cfelse>
            <cfset LOCAL.gmtOffset = session.gmtOffset>
        </cfif>

        <!--- Convert UTC date to users timezone --->
        <cfset LOCAL.newDate = DateAdd("h",LOCAL.gmtOffset,LOCAL.utc_ts)>

        <!--- Format date properly--->
        <cfset LOCAL.dateToDisplay = dateformat(LOCAL.newDate,"mm/dd/yyyy")>

        <!--- If time is needed, tack on time to date--->
        <cfif arguments.displayTime>
            <cfset LOCAL.dateToDisplay = LOCAL.dateToDisplay & ' - ' & timeformat(LOCAL.newDate,"hh:mm tt")>
        </cfif>

        <cfreturn LOCAL.dateToDisplay>
    </cffunction>

0 个答案:

没有答案