ColdFusion 未正确解释 CFDirectory 中的日期时间

发布于 2024-12-18 18:01:43 字数 803 浏览 3 评论 0原文

我们在 Windows Server 2003 上安装了 Coldfusion

未送达邮件上的 CFDirectory 转储返回以下内容:

在此处输入图像描述

但是问题出在迭代此查询时,当我转储日期时:

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

这就是我得到的:

11-Nov-2026
2027 年 11 月 11 日
2028 年 11 月 11 日
2029 年 11 月 11 日
2029 年 11 月 11 日
2029 年 11 月 11 日
1930 年 11 月 11 日
1930 年 11 月 11 日
1930 年 11 月 11 日
1930 年 11 月 11 日
1930 年 11 月 11 日
1930 年 11 月 11 日
1930 年 11 月 11 日
1930 年 11 月 11 日

这样做:

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

now() 是 2011 年 11 月 30 日,可以说下午 2:00 给了我非常奇怪的结果

这只发生在 Windows server 2003(我们的生产服务器)上,它在我的本地系统上运行良好( XP)

有什么想法吗?

We have Coldfusion installed on Windows Server 2003

A dump of CFDirectory on the undelivered mail returns the following:

enter image description here

But the problem is upon iterating this query, when I dump the date with:

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

This is what I get:

11-Nov-2026
11-Nov-2027
11-Nov-2028
11-Nov-2029
11-Nov-2029
11-Nov-2029
11-Nov-1930
11-Nov-1930
11-Nov-1930
11-Nov-1930
11-Nov-1930
11-Nov-1930
11-Nov-1930
11-Nov-1930

So doing:

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

now() being 30th Nov 2011 lets say 2:00 PM gives me very weird results

This only happens on Windows server 2003 (Our production server), it worked fine on my local system (XP)

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冬天的雪花 2024-12-25 18:01:43

我知道这是一个非常古老的线程,但是... cfdirectory 返回一个本地化日期字符串(不是日期对象)。因此,您应该使用 LS(区域设置敏感)日期函数来解析它。原因是标准 CF 日期函数(DateFormatParseDateTime...)始终使用美国日期约定。由于美国惯例是月份在前,因此如果您传入“dd-mm-yyyyy”日期字符串,您将得到错误的结果。 (至少在某些时候。)

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

I know this is a really old thread, but ... cfdirectory returns a localized date string (not date object). So you should be using the LS (locale sensitive) date functions to parse it. The reason is that the standard CF date functions (DateFormat, ParseDateTime, ...) always use U.S. date conventions. Since the U.S. convention is month first, you will get the wrong results if you pass in a "dd-mm-yyyyy" date string. (At least some of the time.)

<cfscript>
   setLocale("en_GB");
   WriteOutput( dateFormat(lsParseDateTime("26/11/11 2:42 PM"), "dd-mmm-yyyy") );
</cfscript>
东京女 2024-12-25 18:01:43

看起来您修改的日期以 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 是 Format 及其子类使用的一个简单类,用于在解析期间跟踪当前位置。

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

Looks like your modified dates are coming out in a format that dateFormat() doesn't recognise.

Try using a java SimpleDateFormat to convert to a cf "{ts}" date.
You create a SimpleDateFormat + ParsePosition, then in your loop, call the sdf.parse() method and reset the position with pp.setIndex(0)

If you want this to only run on your windows 2003 server, check the server scope 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>

Simple demonstration of it working:

<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 is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization.

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

ParsePosition is a simple class used by Format and its subclasses to keep track of the current position during parsing.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文