如何查找最近过去的星期一?

发布于 2024-12-29 07:26:14 字数 661 浏览 4 评论 0 原文

我正在使用 ColdFusion 9.0.1。

我每周一午夜都会举办一场比赛。我需要使用 ColdFusion(但我确信其他语言的逻辑是相同的)来查找最近星期一的日期。一旦确定了该日期,我就会将该日期放入 SQL 语句中以获取当前排名和过去的结果。

那么,我需要什么函数来查找最近过去的星期一呢?

回答

Dates = structNew();
Dates.CurrentDay = dateFormat(now(), "yyyy-mm-dd");
// LOOP MAX OF SEVEN TIMES
for (i = 1; i lte 7; i++) {
    // IF CURRENT DAY OF WEEK IS MONDAY SET AND BREAK
    if (dayOfWeek(Dates.CurrentDay) == 2) {
        Dates.BikeOfTheWeekDate = Dates.CurrentDay;
        break; 
    // IF CURRENT DAY OF WEEK IS NOT MONDAY SUBTRACT DAY
    } else {
        Dates.CurrentDay = dateAdd("d", -1, Dates.CurrentDay);
    }
}

I am using ColdFusion 9.0.1.

I am creating a contest each Monday at midnight. I need to use ColdFusion (but I am sure the logic is the same for other languages) to find the date of the most recent past Monday. Once I determine that date, I will drop that date into a SQL Statement to get the current standings and past results.

So, what functions do I need to find the most recent past Monday?

ANSWER

Dates = structNew();
Dates.CurrentDay = dateFormat(now(), "yyyy-mm-dd");
// LOOP MAX OF SEVEN TIMES
for (i = 1; i lte 7; i++) {
    // IF CURRENT DAY OF WEEK IS MONDAY SET AND BREAK
    if (dayOfWeek(Dates.CurrentDay) == 2) {
        Dates.BikeOfTheWeekDate = Dates.CurrentDay;
        break; 
    // IF CURRENT DAY OF WEEK IS NOT MONDAY SUBTRACT DAY
    } else {
        Dates.CurrentDay = dateAdd("d", -1, Dates.CurrentDay);
    }
}

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

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

发布评论

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

评论(3

对你再特殊 2025-01-05 07:26:14

您也可以从 2 中减去当前星期几(即星期一)

<!--- All days except Sunday (ie 2-Monday,...,7-Saturday) --->
<cfif dayOfWeek(currentDate) gt 1>
    <cfset mostRecentMonday = dateAdd("d", 2-dayOfWeek(currentDate), currentDate)>
<cfelse>
    <cfset mostRecentMonday = dateAdd("d", -6, currentDate)>
</cfif>

You could also just deduct the current day of week from 2 (ie Monday)

<!--- All days except Sunday (ie 2-Monday,...,7-Saturday) --->
<cfif dayOfWeek(currentDate) gt 1>
    <cfset mostRecentMonday = dateAdd("d", 2-dayOfWeek(currentDate), currentDate)>
<cfelse>
    <cfset mostRecentMonday = dateAdd("d", -6, currentDate)>
</cfif>
心奴独伤 2025-01-05 07:26:14

伪代码:

Get the current day
Loop
    Check if it's Monday
        If yes, break out of the loop
    Substract one
Next loop

在 ColdFusion 中,使用 DateAdd("d", -1, date) 减去一天,并使用 DayOfWeek(date) 检查周一,周一返回 2。

Pseudocode:

Get the current day
Loop
    Check if it's Monday
        If yes, break out of the loop
    Substract one
Next loop

In ColdFusion, substract one day with DateAdd("d", -1, date) and check for Monday with DayOfWeek(date) which returns 2 for Monday.

残花月 2025-01-05 07:26:14

您还可以使用 java Calendar 类来完成此操作。

这是 ColdFusion 日期方法内部使用的。

<cfscript>
var cal = createObject( 'java', 'java.util.Calendar' ).getInstance();
cal.setTime( now() );

// if the (7) day of week is before (2) monday, we want the previous week
// decrement the (3) week of year
if ( cal.get(7) < 2 ) {
    cal.set( 3, cal.get(3) - 1 );
}

// set the (7) day of week back to (2) monday
cal.set( 7, 2 );    // 7 = day of week, 2 = monday

// reset time fields back to 0, aka midnight
cal.set( 11, 0 );   // 11 = hour of day
cal.set( 12, 0 );   // 12 = minute
cal.set( 13, 0 );   // 13 = second
cal.set( 14, 0 );   // 14 = millisecond

// get the last monday Date
var lastMonday = cal.getTime();

// cal.getTime() returns a java.util.Date
// if you want to convert the date to a ColdFusion OleDateTime, you can like so
var lastMonday = dateAdd( 'd', 0, cal.getTime() );

// or like this
var lastMonday = createObject( 'java', 'coldfusion.runtime.OleDateTime' ).init( cal.getTime() );
<cfscript>

http://docs.oracle.com/javase /1.5.0/docs/api/java/util/Calendar.html

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Date.html

You can also do it using the java Calendar class.

Which is what the ColdFusion date methods use internally.

<cfscript>
var cal = createObject( 'java', 'java.util.Calendar' ).getInstance();
cal.setTime( now() );

// if the (7) day of week is before (2) monday, we want the previous week
// decrement the (3) week of year
if ( cal.get(7) < 2 ) {
    cal.set( 3, cal.get(3) - 1 );
}

// set the (7) day of week back to (2) monday
cal.set( 7, 2 );    // 7 = day of week, 2 = monday

// reset time fields back to 0, aka midnight
cal.set( 11, 0 );   // 11 = hour of day
cal.set( 12, 0 );   // 12 = minute
cal.set( 13, 0 );   // 13 = second
cal.set( 14, 0 );   // 14 = millisecond

// get the last monday Date
var lastMonday = cal.getTime();

// cal.getTime() returns a java.util.Date
// if you want to convert the date to a ColdFusion OleDateTime, you can like so
var lastMonday = dateAdd( 'd', 0, cal.getTime() );

// or like this
var lastMonday = createObject( 'java', 'coldfusion.runtime.OleDateTime' ).init( cal.getTime() );
<cfscript>

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Date.html

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