将八位数字转换为日期的最有效方法是什么?

发布于 2024-12-16 11:59:29 字数 1219 浏览 2 评论 0原文

我正在使用 ColdFusion 9.0.1 和一些我无法更改的数据库。

我正在访问一个数据库,该数据库将日期存储为小数点后为零的八位数字,如下所示:

YYYYMMDD

我需要能够读取日期、在日期中添加和减去天数以及创建新日期。我正在寻找一个 ColdFusion 解决方案来有效(不需要太多代码)将日期转换为我们的标准格式,

MM/DD/YYYY

然后将其转换回数据库的格式以进行保存。

我需要以这样的方式进行编码,以便非 ColdFusion 程序员可以轻松阅读并使用它,复制它并修改它以用于其他功能(例如在日期中添加一天)。所以,我不是在寻找最少的代码量,而是高效且可读的代码。

您能否提出任何建议,使该代码块更灵活、更易读或更高效(更少的代码)?

<cfscript>

// FORMAT DB DATE FOR BROWSER
DateFromDB = "20111116";
DatedToBrowser = createBrowserDate(DateFromDB);
writeOutput(DatedToBrowser);

function createBrowserDate(ThisDate) {
    ThisYear = left(ThisDate, 4); 
    ThisMonth = mid(ThisDate, 4, 2);
    ThisDay = right(ThisDate, 2);
    NewDate = createDate(ThisYear, ThisMonth, ThisDay);
    NewDate = dateFormat(NewDate, "MM/DD/YYYY");
    return NewDate;
}

// FORMAT BROWSER DATE FOR DB
DateFromBrowser = "11/16/2011";
DateToDB = createDBDate(DateFromBrowser);
writeDump(DateToDB);

function createDBDate(ThisDate) {
    ThisYear = year(ThisDate); 
    ThisMonth = month(ThisDate);
    ThisDay = day(ThisDate);
    NewDate = "#ThisYear##ThisMonth##ThisDay#";
    return NewDate;
}

</cfscript>

I am using ColdFusion 9.0.1 and some database that I cannot change.

I am accessing a database that stores a date as an eight digit numeric with zero decimal places like this:

YYYYMMDD

I need to be able to read the date, add and subtract days from a date, and create new dates. I am looking for a ColdFusion solution to efficiently (not much code) to convert the date to our standard format, which is

MM/DD/YYYY

And then convert it back into the database's format for saving.

I need to code this in such a way that non-ColdFusion programmers can easily read this and use it, copy and modify it for other functions (such as adding a day to a date). So, I am not looking for the most least amount of code, but efficient and readable code.

Can you suggest anything that would make this code block more flexible, readable, or more efficient (less code)?

<cfscript>

// FORMAT DB DATE FOR BROWSER
DateFromDB = "20111116";
DatedToBrowser = createBrowserDate(DateFromDB);
writeOutput(DatedToBrowser);

function createBrowserDate(ThisDate) {
    ThisYear = left(ThisDate, 4); 
    ThisMonth = mid(ThisDate, 4, 2);
    ThisDay = right(ThisDate, 2);
    NewDate = createDate(ThisYear, ThisMonth, ThisDay);
    NewDate = dateFormat(NewDate, "MM/DD/YYYY");
    return NewDate;
}

// FORMAT BROWSER DATE FOR DB
DateFromBrowser = "11/16/2011";
DateToDB = createDBDate(DateFromBrowser);
writeDump(DateToDB);

function createDBDate(ThisDate) {
    ThisYear = year(ThisDate); 
    ThisMonth = month(ThisDate);
    ThisDay = day(ThisDate);
    NewDate = "#ThisYear##ThisMonth##ThisDay#";
    return NewDate;
}

</cfscript>

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

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

发布评论

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

评论(5

十雾 2024-12-23 11:59:29

首先找到谁曾经做过数据库并将他们踢到nads...

就我个人而言,我会使用sql进行转换,所以我的代码只处理日期对象。

Select Convert(DateTime, Convert(VarChar(8),DateTimeInventedByIdjitColumn))
From SomeTable

正如我们的同行所说,将日期存储为日期。

“08/06/2011”可能是 6 月 8 日或 8 月 6 日,具体取决于区域设置。

20111643 是一个有效的整数。

不使用正确的日期类型只是大量功能和错误的集合,充其量只是等待发生。

First find who ever did the database and kick them in the nads...

Personally I'd Convert with sql so my code only dealt with date objects.

Select Convert(DateTime, Convert(VarChar(8),DateTimeInventedByIdjitColumn))
From SomeTable

As stated by our peers, store dates as dates.

'08/06/2011' could be 8th of june of the 6th of August depending on locale.

20111643 is a valid integer..

Not using a proper date type is just a massive collection of features and bugs that at best are waiting to happen.

意中人 2024-12-23 11:59:29

您实际上可以将每个函数重写为 1 行代码。

function createBrowserDate(ThisDate) {
  return mid(ThisDate,4,2) & "/" & right(ThisDate,2) & "/" & left(ThisDate,4);
}

function createDBDate(ThisDate) {
  return dateFormat( ThisDate, "YYYYMMDD" );
}

You can actually rewrite each function into 1 line of code.

function createBrowserDate(ThisDate) {
  return mid(ThisDate,4,2) & "/" & right(ThisDate,2) & "/" & left(ThisDate,4);
}

and

function createDBDate(ThisDate) {
  return dateFormat( ThisDate, "YYYYMMDD" );
}
挽容 2024-12-23 11:59:29

不要将日期保留为字符串 - 将日期保留为日期,并在需要时设置它们的格式。

如果您无法更正数据库以使用实际日期列(如果可以,您应该这样做),那么您可以使用这两个函数在 YYYYMMDD 和日期对象之间进行转换:

function parseYMD( YYYYMMDD )
{
    if ( ! refind('^\d{8}

通过使用日期对象,意味着任何级别的开发人员都可以通过 等内置函数使用它们,而无需关心格式。 DateAddDateCompare 等。

, Arguments.YYYYMMDD ) ) throw "Invalid Format. Expected YYYYMMDD"; return parseDateTime ( Arguments.YYYYMMDD.replaceAll('(?<=^\d{4})|(?=\d{2}$)','-') ); } function formatYMD( DateObj ) { return DateFormat( DateObj , 'yyyymmdd' ); }

通过使用日期对象,意味着任何级别的开发人员都可以通过 等内置函数使用它们,而无需关心格式。 DateAddDateCompare 等。

Don't keep dates as strings - keep dates as dates and format them when you need to.

If you can't correct the database to use actual date columns (which you should if you can), then you can use these two functions to convert to/from YYYYMMDD and a date object:

function parseYMD( YYYYMMDD )
{
    if ( ! refind('^\d{8}

By using date objects it means that any level of developer can work with them, without needing to care about formatting, via built-in functions like DateAdd, DateCompare, and so on.

, Arguments.YYYYMMDD ) ) throw "Invalid Format. Expected YYYYMMDD"; return parseDateTime ( Arguments.YYYYMMDD.replaceAll('(?<=^\d{4})|(?=\d{2}$)','-') ); } function formatYMD( DateObj ) { return DateFormat( DateObj , 'yyyymmdd' ); }

By using date objects it means that any level of developer can work with them, without needing to care about formatting, via built-in functions like DateAdd, DateCompare, and so on.

各自安好 2024-12-23 11:59:29

我不是正则表达式的粉丝,因为它对我来说不太可读。

由于您使用的是 CF9,因此我输入了参数并指定了函数的返回类型,以便下一个人拿起您的代码时更具可读性。

首先,在我从数据库中读取日期后,我会使用 parseDBDate() 将其解析为 Date 对象。

Date function parseDBDate(required String dbDate)
{
    var yyyy = left(dbDate, 4); 
    var mm = mid(dbDate, 4, 2);
    var dd = right(dbDate, 2);

    return createDate(yyyy , mm, dd);
}

一旦获得日期对象,您就可以使用所有内置的 Date 函数,例如 <代码>DateAdd() 或DateDiff()

在需要显示它之前调用 browserDateFormat()

String function browserDateFormat(required Date date) 
{
    return dateFormat(date, "MM/DD/YYYY");
}

当需要持久化到数据库时,在 内调用 dBDateFormat()

String function dBDateFormat(required Date date) 
{
    return dateFormat(date, "YYYYMMDD");
}

I'm not a regular expression fan since it's not that readable to me.

Since you're using CF9, I'd typed the argument and specify the returntype of the functions to be even more readable for the next person picking up your code.

First, right after I read the date from DB, I'd parse it to a Date object using parseDBDate()

Date function parseDBDate(required String dbDate)
{
    var yyyy = left(dbDate, 4); 
    var mm = mid(dbDate, 4, 2);
    var dd = right(dbDate, 2);

    return createDate(yyyy , mm, dd);
}

Once you have the date object, you can use all those built-in Date functoin like DateAdd() or DateDiff().

Call browserDateFormat() right before you need to display it.

String function browserDateFormat(required Date date) 
{
    return dateFormat(date, "MM/DD/YYYY");
}

Call dBDateFormat() inside <cfqueryparam value=""> when it's time to persist to DB

String function dBDateFormat(required Date date) 
{
    return dateFormat(date, "YYYYMMDD");
}
妄司 2024-12-23 11:59:29

一个衬垫 :)

myDateString = "20110203";

myCfDate = createObject("java","java.text.SimpleDateFormat").init("yyyyMMdd").parse(myDateString,createObject("java","java.text.ParsePosition").init(0*0));

如果您想解析不同的模式,请将“yyyyMMdd”更改为任何其他支持的模式。

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

ParsePosition 用于说明从哪里开始解析字符串。
0*0 是 JavaCast("int",0) 的简写 - 在 Adob​​e cf 引擎中,0 是一个字符串,直到您对其应用数学,然后它就变成了 ParsePosition 构造函数支持的 Double。从技术上讲,它是用 int 构造的,但 cf 足够聪明,可以将 Double 降级为 int。

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

One liner :)

myDateString = "20110203";

myCfDate = createObject("java","java.text.SimpleDateFormat").init("yyyyMMdd").parse(myDateString,createObject("java","java.text.ParsePosition").init(0*0));

If you want to parse different patterns, change "yyyyMMdd" to any other supported pattern.

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

The ParsePosition is used to say where to start parsing the string.
0*0 is shorthand for JavaCast("int",0) - in the Adobe cf engine, 0 is a string, until you apply math to it, then it becomes a Double, which the ParsePosition constructor supports. Technically, it constructs with an int, but cf is smart enough to downgrade a Double to an int.

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

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