本文介绍了Coldfusion 10 日期格式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 DateFormat 函数将日期转换为这种格式:yyyy-mm-dd.这是日期的原始格式:dd-mm-yyyy.下面是一段代码:

I am using the DateFormat function to convert dates to this format: yyyy-mm-dd. This is the original format of the date: dd-mm-yyyy. Below is a snippet of the code:

<cfset newdate = #DateFormat(Trim(mydate), "yyyy-mm-dd")# />

问题是我在不同的日期得到不同的结果.例如:

The problem is that I get different results for different dates. For example:

  • 如果我的原始日期是:15-05-2013 (dd-mm-yyyy)
  • 结果是:2013-05-15 (yyyy-mm-dd)
  • If my original date is: 15-05-2013 (dd-mm-yyyy)
  • The result is: 2013-05-15 (yyyy-mm-dd)

但是,如果我更改输入并且:

However, if I change the input and:

  • 原日期为:01-05-2013 (dd-mm-yyyy)
  • 结果是:2013-01-05 (yyyy-dd-mm)
  • The original date is: 01-05-2013 (dd-mm-yyyy)
  • The result is: 2013-01-05 (yyyy-dd-mm)

我们非常感谢任何有关问题的帮助或指导.

Any help or guidance as to what is wrong would be highly appreciated.

推荐答案

我不同意另一个答案.问题的真正原因是 DateFormat 并非旨在处理非美国日期字符串.

I disagree with the other answer. The real cause of the problem is that DateFormat is not designed to handle non-US date strings.

标准 CF 日期函数总是使用美国日期解析规则.这意味着当您传入一个模棱两可的日期字符串时,例如 01-05-2013,它会根据 美国英文日期约定.在这种情况下,首先是月份,即mm-dd-yyyy".所以结果永远是 1 月 5 日,而不是 5 月 1 日.

The standard CF date functions always use U.S. date parsing rules. That means when you pass in an ambiguous date string, like 01-05-2013, it is parsed according to U.S. English date conventions. In this case, month first ie "mm-dd-yyyy". So the result will always be January 5th, not May 1st.

在某些情况下,您会很幸运.对于字符串 15-05-2013,显然没有第 15 个月,所以 CF/java 必须自动交换月份和日期,而不是抛出错误.这就是为什么 似乎 可以正确处理一些 dd-mm-yyyy 日期字符串,但不能正确处理其他日期字符串.

In some cases you get lucky. With the string 15-05-2013, there is obviously no 15th month, so CF/java must swap the month and day automatically, rather than throwing an error. That is why it seems to handle some dd-mm-yyyy date strings correctly, but not others.

如果要解析非美国日期字符串,则应改用 LS(Locale Sensitive)日期函数.但是,根据文档,破折号即 "-" 在大多数非美国语言环境中不是标准的日期分隔符:只有荷兰语和葡萄牙语(标准).因此,您要么需要更改分隔符,要么在解析日期时使用这两种语言环境之一:

If you want to parse non-US date strings, you should use the LS (Locale Sensitive) date functions instead. However, according to the docs dashes ie "-" are not a standard date separator in most non-US locales: only Dutch and Portuguese (Standard). So you would either need to change the separator OR use one of those two locales when parsing the date:

        lsDateFormat( myDate, "yyyy-mm-dd", "pt_PT")

旁注:

顺便说一句,DateFormat 确实需要一个日期对象.然而,像 CF 中的大多数函数一样,它也足够灵活,可以接受日期 string.这使您可以将其用作从日期 string => 日期 object => 然后再次转换回(格式化)日期字符串的惰性快捷方式.最好使用日期对象(你真的应该验证日期字符串),但这完全是另一个对话......

As an aside, DateFormat does expect a date object. However, like most functions in CF it is flexible enough to accept a date string as well. That allows you to use it as a lazy shortcut to convert from date string => date object => then back to (formatted) date string again. Using date objects is preferable (and you really should validate date strings as well) but that is another conversation altogether ...

这篇关于Coldfusion 10 日期格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 14:33