是否有一个函数可以将date创建的Twitter API转换为ColdFusion 8+;的正确格式;?

是否有一个函数可以将date创建的Twitter API转换为ColdFusion 8+;的正确格式;?,date,coldfusion,twitter,Date,Coldfusion,Twitter,Twitter API使用以下格式的JSON格式从API.Twitter.com追溯日期(创建时间): “2010年12月10日星期五17:12:00+0000”(try 函数twitterDate(日期、偏移量){ var retDate=listtoarray(日期“”); var thisDay=归还日期[1]; 本月风险值=归还日期[2]; var thisDate=retDate[3]; var ThistTime=时间格式(返回日期[4],“h:mm tt”); 本年风险值=归还日

Twitter API使用以下格式的JSON格式从API.Twitter.com追溯日期(创建时间):

“2010年12月10日星期五17:12:00+0000”(try


函数twitterDate(日期、偏移量){
var retDate=listtoarray(日期“”);
var thisDay=归还日期[1];
本月风险值=归还日期[2];
var thisDate=retDate[3];
var ThistTime=时间格式(返回日期[4],“h:mm tt”);
本年风险值=归还日期[6];
var thisReturn=“”;
var thisFormat=“#本月#,#本日期##本年#”;
thisFormat=dateformat(thisFormat,“m/d/yy”)和“&thisTime;
thisFormat=日期添加(“s”,偏移量,thisFormat);
thisFormat=dateadd(“h”,1,thisFormat);
longFormat=dateformat(thisFormat,“yyyy-mm-dd”)&“&timeformat(thisFormat,“HH:mm:ss”);
thisReturn=longFormat;
返回此返回;
}

您可以轻松地将该字符串视为一个空格分隔的列表,并组成一个更友好的字符串。由于数组比列表快,因此我将尽可能快地将其放入一个数组中,然后从中进行操作

public string function getSaneTwitterDate(strDateIn) output="false"{
  var arrOrigDate = listToArray(strDateIn, ' ');
  var strNewDate = arrOrigDate[2] & ' ' & arrOrigDate[3] & ' ' & arrOrigDate[6];
  return dateFormat(strNewDate, 'yyyy-mm-dd');
}
这不考虑时间偏移或包含时间信息,但很容易添加。


<cffunction name="parseTwitterDateFormat" output="false" returntype="String" hint="I return a date in a useable date format.">
    <cfargument name="twitterDate" required="true" type="string" hint="The Twitter date." />

    <cfset var formatter = CreateObject("java", "java.text.SimpleDateFormat").init("EEE MMM d kk:mm:ss Z yyyy") />
    <cfset formatter.setLenient(true) />

    <cfreturn formatter.parse(arguments.twitterDate) />
</cffunction>

马特·吉福德(Matt Gifford)的monkeyTweet库(monkeyTweet library)

真的吗?你不必费心复制代码并粘贴到你的答案中吗?自从我在twitter上回答他以来,它已经在pastebin中了。为试图帮助将来可能搜索并来到这里的其他人表示歉意。问题是,linkrot会发生。如果出于某种原因,该链接会被删除Ange或走开,这个答案绝对没有价值。最好在目标位置复制(或摘录)信息,并使用链接将其来源。为什么函数中有偏移量参数?这样说很简单:D我通过添加“&”&arrOrigDate[4]对其进行了轻微修改返回值的末尾,以便它也包含时间…感谢Adam!更新了它,以便从搜索API
公共字符串函数getSaneTwitterDate(strDateIn)output=“false”{var arrOrigDate=listToArray(arguments.strDateIn');var strNewDate=arrOrigDate[2]&''&arrOrigDate[3]正确解析Twitter日期&“”;//来自api.twitter.com if(IsNumeric(arrOrigDate[3]){strNewDate&=arrOrigDate[6];return dateFormat(strNewDate,'yyyyy-mm-dd')&'&arroridate[4];//来自search.twitter.com}否则{strNewDate&=arroridate[4];return dateFormat(strNewDate,'yyyyy-mm-dd')&'&arroridate[5];})
setLenient(true)的酷之处在于,它会导致格式化程序自动将无效日期(如“2010年2月30日”)调整为有效日期(如“2010年3月2日”)。如果您想要更严格的解析,只需将其设置为
false
<cffunction name="parseTwitterDateFormat" output="false" returntype="String" hint="I return a date in a useable date format.">
    <cfargument name="twitterDate" required="true" type="string" hint="The Twitter date." />

    <cfset var formatter = CreateObject("java", "java.text.SimpleDateFormat").init("EEE MMM d kk:mm:ss Z yyyy") />
    <cfset formatter.setLenient(true) />

    <cfreturn formatter.parse(arguments.twitterDate) />
</cffunction>