ColdFusion - DateTime Format with GMT offset

一曲冷凌霜 提交于 2019-12-13 15:25:39

问题


I am using ColdFusion 10 to make some REST calls and the date returned is using a GMT offset.

Example: 2013-03-25T14:30:40-04:00

I need this formatted for 2 purposes:

  1. Screen Display so it looks something like mm/dd/yyyy hh:mm:ss
  2. To Insert into mySQL.

I have tried a variety of the CF time/date functions but continue to get the "is not a valid date format"

I thought maybe the #ParseDateTime(i.submitted_at,"pop")# would handle it with POP but same issue.

Spent a few hours now trying multiple variations and googling around now just going in circles. Any ideas would be greatly appreciated.

Thanks!


回答1:


Have a look at the UDF DateConvertISO8601() on CFLib.

DateConvertISO8601(ISO8601dateString, targetZoneOffset) on CFLib

Description:
This function take a string that holds a date in ISO 8601 and converts it to ODBC datetime, but could be adapted to convert to whatever you like. It also will convert to a datetime in a timezone of your choice by specifying the offset, i.e. it could take a datetime in GMT and convert to PT. See http://www.w3.org/TR/NOTE-datetime for description of ISO 8601, the International Standard for the representation of dates and times.

Return Values:
Returns a datetime.

The source code is viewable at the link I provided.




回答2:


This, 2013-03-25T14:30:40-04:00, is a string. If you run this:

x = left(2013-03-25T14:30:40-04:00, 19);

you get 2013-03-25T14:30:40. You can use the replace function to replace the T with a space. You can then to this

DateTimeVar =parsedatetime('2013-03-25 14:30:40');

Now you have a datetime variable that you can format. If necessary you can do a datediff with the offset from GMT.




回答3:


With a number of remote requests and responses, the date / time values can often be returned in ISO format. In your case, the mask looks like this:

YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)

In this ISO format, the T string is a literal representation of a marker where the time stamp starts in the string (with the offset following directly).

Below is a reusable function that will convert an ISO date format into a useable ColdFusion date time object:

<cffunction name="ISOToDateTime" access="public" returntype="string" output="false" 
    hint="Converts an ISO 8601 date/time stamp with optional dashes to a ColdFusion   
        date/time stamp.">
    <cfargument name="Date" type="string" required="true" hint="ISO 8601 date/time stamp." />
        <cfreturn ARGUMENTS.Date.ReplaceFirst(
            "^.*?(\d{4})-?(\d{2})-?(\d{2})T([\d:]+).*$",
            "$1-$2-$3 $4"
            ) />
</cffunction>

You can then call the function like so to output or return a ColdFusion-friendly version of the date time:

ISOToDateTime( "2013-03-25T14:30:40-04:00" )

That function is courtesy of Ben Nadel. The original blog post can be found here:

http://www.bennadel.com/blog/811-Converting-ISO-Date-Time-To-ColdFusion-Date-Time.htm

You can also convert the date time value using the offset, if required. Again, Ben Nadel has a great blog post outlining how to accomplish this:

http://www.bennadel.com/blog/1595-Converting-To-GMT-And-From-GMT-In-ColdFusion-For-Use-With-HTTP-Time-Stamps.htm




回答4:


This is an informational answer, not a direct answer to the question.

ColdFusion 11 has updated the ParseDateTime() function so that it will correctly convert the ISO-8601 date/time strings to a ColdFusion datetime object.




回答5:


CF10 can use this code as stated in the example of the parseDateTime() doc.

<cfset string = "1997-07-16T19:20:30+01:00">
<cfset date = parseDateTime(string, "yyyy-MM-dd'T'HH:mm:ssX")>


来源:https://stackoverflow.com/questions/15658756/coldfusion-datetime-format-with-gmt-offset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!