# Return date of last Sunday

## Recommended Posts

Trying to work out a formula to return the date of the previous Sunday, unless the date entered is a Sunday. I don't see anything in Date.au3 to do this, unfortunately.

Any help is appreciated.

##### Share on other sites

Something like this?

`\$Last_Sundays_date=_DateAdd( 'd',-_DateToDayOfWeek (@YEAR, @MON, @MDAY)+1, _NowCalc())`

Live for the present,
Dream of the future,
Learn from the past.

##### Share on other sites

Trying to work out a formula to return the date of the previous Sunday, unless the date entered is a Sunday. I don't see anything in Date.au3 to do this, unfortunately.

Any help is appreciated.

```#include <Date.au3>
Global \$lastsundaydate

\$inputdate = "00/00/0000";MM/DD/YYYY
\$inputdatearray = StringSplit(\$inputdate, "/")
\$inputdayofweek = _DateToDayOfWeek(\$inputdatearray[3], \$inputdatearray[1], \$inputdatearray[2])
If @ERROR <> 1 Then
Select
Case \$inputdayofweek = 1
MsgBox(48,"","The date entered is a Sunday!")
Case \$inputdayofweek = 2
\$lastsundaydate = _DateAdd("D", -1, \$inputdatearray[3] & "/" & \$inputdatearray[1] & "/" & \$inputdatearray[2])
Case \$inputdayofweek = 3
\$lastsundaydate = _DateAdd("D", -2, \$inputdatearray[3] & "/" & \$inputdatearray[1] & "/" & \$inputdatearray[2])
Case \$inputdayofweek = 4
\$lastsundaydate = _DateAdd("D", -3, \$inputdatearray[3] & "/" & \$inputdatearray[1] & "/" & \$inputdatearray[2])
Case \$inputdayofweek = 5
\$lastsundaydate = _DateAdd("D", -4, \$inputdatearray[3] & "/" & \$inputdatearray[1] & "/" & \$inputdatearray[2])
Case \$inputdayofweek = 6
\$lastsundaydate = _DateAdd("D", -5, \$inputdatearray[3] & "/" & \$inputdatearray[1] & "/" & \$inputdatearray[2])
Case \$inputdayofweek = 7
\$lastsundaydate = _DateAdd("D", -6, \$inputdatearray[3] & "/" & \$inputdatearray[1] & "/" & \$inputdatearray[2])
EndSelect
If \$lastsundaydate <> "" Then
MsgBox(0,"Previous Sunday's Date","The last Sunday prior to the date entered was " & \$lastsundaydate & ".")
EndIf
EndIf```

Certifications: A+, Network+, Security+, Linux+, LPIC-1, MCSA | Languages: AutoIt, C, SQL, .NETBooks: AutoIt v3: Your Quick Guide - \$7.99 - O'Reilly Media - September 2007-------->[u]AutoIt v3 Development - newbie to g33k[/u] - Coming Soon - Fate Publishing - Spring 2013UDF Libraries: SkypeCOM UDF Library | ADUC Computers OU Cleanup | Find PixelChecksumExamples: Skype COM Examples - Skype4COMLib Examples converted from VBS to AutoIt

##### Share on other sites

```#include <Date.au3>
Global \$lastsundaydate

\$inputdate = "00/00/0000";MM/DD/YYYY
\$inputdatearray = StringSplit(\$inputdate, "/")
\$inputdayofweek = _DateToDayOfWeek(\$inputdatearray[3], \$inputdatearray[1], \$inputdatearray[2])
If @ERROR <> 1 Then
Select
Case \$inputdayofweek = 1
MsgBox(48,"","The date entered is a Sunday!")
Case \$inputdayofweek = 2
\$lastsundaydate = _DateAdd("D", -1, \$inputdatearray[3] & "/" & \$inputdatearray[1] & "/" & \$inputdatearray[2])
Case \$inputdayofweek = 3
\$lastsundaydate = _DateAdd("D", -2, \$inputdatearray[3] & "/" & \$inputdatearray[1] & "/" & \$inputdatearray[2])
Case \$inputdayofweek = 4
\$lastsundaydate = _DateAdd("D", -3, \$inputdatearray[3] & "/" & \$inputdatearray[1] & "/" & \$inputdatearray[2])
Case \$inputdayofweek = 5
\$lastsundaydate = _DateAdd("D", -4, \$inputdatearray[3] & "/" & \$inputdatearray[1] & "/" & \$inputdatearray[2])
Case \$inputdayofweek = 6
\$lastsundaydate = _DateAdd("D", -5, \$inputdatearray[3] & "/" & \$inputdatearray[1] & "/" & \$inputdatearray[2])
Case \$inputdayofweek = 7
\$lastsundaydate = _DateAdd("D", -6, \$inputdatearray[3] & "/" & \$inputdatearray[1] & "/" & \$inputdatearray[2])
EndSelect
If \$lastsundaydate <> "" Then
MsgBox(0,"Previous Sunday's Date","The last Sunday prior to the date entered was " & \$lastsundaydate & ".")
EndIf
EndIf```
sexy

[u][font="Century Gothic"]~я α и d γ ĵ . ċ . ѕ қ ϊ и и ε я~- My Programs -auto shutdownSleep funcdisallow programs[/font][/u]

##### Share on other sites

Something like this?

`\$Last_Sundays_date=_DateAdd( 'd',-_DateToDayOfWeek (@YEAR, @MON, @MDAY)+1, _NowCalc())`
Thanks!

##### Share on other sites

Trying to work out a formula to return the date of the previous Sunday, unless the date entered is a Sunday. I don't see anything in Date.au3 to do this, unfortunately.

Any help is appreciated.

Check it out!

```#include <Date.au3>
\$Date = InputBox("Date", "Input a date in the following format:"&@CRLF&"MM/DD/YYYY", @MON&"/"&@MDAY&"/"&@YEAR)
\$Temp = StringSplit(\$Date,"/")
\$Month = \$Temp[1]
\$day = \$Temp[2]
\$Year = \$Temp[3]
\$WDay = CalcWeekday(\$Month, \$Day, \$Year)
\$DayName = CalcWeekday(\$Month, \$Day, \$Year,0,1)
If \$WDay>1 Then
\$sJulDate = _DateToDayValue (\$Year, \$Month, \$Day)
Dim \$Y, \$M, \$D
\$sJulDate = _DayValueToDate (\$sJulDate-\$WDay, \$Y, \$M, \$D)
MsgBox(4096, "", "You entered " & \$DayName &" "& \$Date &@CRLF& "The previous Sunday was "&\$WDay&" days ago: " & \$M & "/" & \$D & "/" & \$Y)
Else
MsgBox(0,"", "You entered " & \$DayName &" "& \$Date)
EndIf

Func CalcWeekday(\$Month,\$Day,\$Year,\$Cal=0,\$Name=0)
\$a = Floor((14 - \$month) / 12)
\$y = \$year - \$a
\$m = \$month + 12*\$a - 2
\$gregorian = Mod(\$day + \$y + Floor(\$y/4) - Floor(\$y/100) + Floor(\$y/400) + Floor((31*\$m)/12),7);gregorian
\$julian = Mod(5 + \$day + \$y + Floor(\$y/4) + Floor((31*\$m)/12),7);julian
If \$Cal = 0 Then
\$Num = Floor(\$gregorian)
Else
\$Num =Floor(\$julian)
EndIf
Dim \$Day[7] = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
If \$Name then Return \$Day[\$Num]
Return \$Num
EndFunc```

##### Share on other sites

What's wrong with my one-liner ? too simple ?

Edited by Jos

Live for the present,
Dream of the future,
Learn from the past.

##### Share on other sites

What's wrong with my one-liner ? too simple ?

Turtles.

##### Share on other sites

What's wrong with my one-liner ? too simple ?

I read it as the poster wanting to input a date. Your one-liner works great, but you'd have to add a few more lines if you wanted to base it on a user input date.

Certifications: A+, Network+, Security+, Linux+, LPIC-1, MCSA | Languages: AutoIt, C, SQL, .NETBooks: AutoIt v3: Your Quick Guide - \$7.99 - O'Reilly Media - September 2007-------->[u]AutoIt v3 Development - newbie to g33k[/u] - Coming Soon - Fate Publishing - Spring 2013UDF Libraries: SkypeCOM UDF Library | ADUC Computers OU Cleanup | Find PixelChecksumExamples: Skype COM Examples - Skype4COMLib Examples converted from VBS to AutoIt

##### Share on other sites

What's wrong with my one-liner ? too simple ?

More is better?

Why use one line when you can easily use a hundred lines?

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

##### Share on other sites

More is better?

Depends on the "matter at hand"

Why use one line when you can easily use a hundred lines?

Simple: Less typing. I am the lazy type.

Live for the present,
Dream of the future,
Learn from the past.

## Create an account

Register a new account