2011-11-30 91 views
3

我們已經安裝了Windows Server 2003日期時間從CFDirectory不被ColdFusion的

CFDirectory對未發送郵件轉儲上ColdFusion的正確解釋返回如下:

enter image description here

但問題是在迭代此查詢,當我轉儲日期:

#dateFormat(mailStubs.DateLastModified,'dd-mmm-yyyy')# 

這就是我得到的:

11-NOV-2026
11-NOV-2027
11-NOV-2028
11-NOV-2029
11-NOV-2029
11-NOV-2029
11新手覺得1930
11月11日1930年
11月11日1930年
11月11日1930年
11月11日1930年
11月11日1930年
11號V-1930
11月11日1930年

這樣算下來:

datediff("n", mailStubs.DateLastModified, now()) 

現在()是30日2011年11月可以說,2:00 PM讓我非常奇怪的結果

這隻發生在Windows Server 2003(我們的生產服務器)上,它在我的本地系統(XP)上運行正常

任何想法?

回答

0

看起來您的修改日期以dateFormat()無法識別的格式顯示。

嘗試使用java SimpleDateFormat將其轉換爲cf「{ts}」日期。 你創建一個SimpleDateFormat + ParsePosition,然後在你的循環中,調用sdf.parse()方法並用pp.setIndex(0)重置位置。

如果你想讓它只在你的Windows 2003服務器上運行,服務器範圍server.os.version

<cfscript> 
// init the class with a pattern that matches your wacky date string 
// do this before you start looping 
var sdf = createObject('java','java.text.SimpleDateFormat').init('dd/MM/yy HH:mm a'); 

// init a parse position, so the above class knows it's position during parsing 
var pp = createObject('java','java.text.ParsePosition').init(0); 

// run your loop 
for (var i = 1; i lte query.recordCount; i++) { 

    // convert the string date into a cf {ts} date. 
    cfdate = sdf.parse(query.myColumn[i], pp); 

    // reset the position for the next .parse() call 
    pp.setIndex(0); 

    // now you can use dateDiff() with your cfdate 
    // if the parse fails, cfdate will be undefined, so check with an isNull() 
} 
</cfscript> 

簡單的演示工作:

<cfscript> 
var dirty = [ 
    '26/11/11 2:42 PM', 
    '27/11/11 10:53 PM', 
    '29/11/11 12:08 AM' 
]; 

var sdf = createObject('java','java.text.SimpleDateFormat').init('dd/MM/yy HH:mm a'); 
var pp = createObject('java','java.text.ParsePosition').init(0); 

var clean = []; 
for (var i = 1; i lte arrayLen(dirty); i++) { 
    clean[i] = sdf.parse(dirty[i], pp); 
    pp.setIndex(0); 
} 
writeDump(var: dirty); 
writeDump(var: clean); 
</cfscript> 

SimpleDateFormat是一個以與語言環境相關的方式來格式化和解析日期的具體類。它允許格式化(日期 - >文本),解析(文本 - >日期)和規範化。

http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html

一個ParsePosition是使用的格式和它的子類,以跟蹤當前位置的解析過程中的簡單類。

http://docs.oracle.com/javase/1.4.2/docs/api/java/text/ParsePosition.html

4

我知道這是一個非常古老的線程,但... cfdirectory返回本地化日期字符串(不是日期對象)。所以你應該使用LS(語言環境敏感)日期函數來解析它。原因在於標準CF日期函數(DateFormatParseDateTime,...)總是使用美國約會。由於美國大會是第一個月,如果您傳遞「dd-mm-yyyyy」日期字符串,則會得到錯誤的結果。 (至少部分時間)

<cfscript> 
    setLocale("en_GB"); 
    WriteOutput(dateFormat(lsParseDateTime("26/11/11 2:42 PM"), "dd-mmm-yyyy")); 
</cfscript> 
相關問題