2011-11-30 29 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> 
相关问题