Jump to content

Recommended Posts

Posted

Hello all,

How to calculate the nearest Sunday?

  • 2012/12/30 (Previous Sunday)
  • 2013/01/03(Today)
  • 2013/01/06(Coming Sunday)
Here considering today's date(2013/01/03) the nearest Sunday is coming Sunday(2013/01/06)

How to achieve this using AutoIt? any function close to this?

Thanks

Posted

chakka,

Are you saying that you want the next Sunday following any date? It may not be the "nearest", for example, if it is Monday the "nearest" Sunday is the previous Sunday.

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Posted

....for example, if it is Monday the "nearest" Sunday is the previous Sunday.

Yes exactly, and I'm looking the same.

If it's Monday then then nearest Sunday is previous Sunday(date?) and If it's Friday then nearest Sunday is coming Sunday(date?)

All I want to get is the date of that day.

Thanks kylomas, can you please provide rough example.

Posted

chakka,

This will get you started

; get closest Sunday from whatever date this is run

#include <date.au3>

if @wday <= 4 then
    local $Sunday = _dateadd('D',-(7-@wday),_NowCalcDate())
    ConsoleWrite($Sunday & @LF)
Else
    local $Sunday = _dateadd('D',(7-@wday)+1,_NowCalcDate())
    ConsoleWrite($Sunday & @LF)
endif

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Posted (edited)

Here a little example:

#include <date.au3>
#include <DateTimeConstants.au3>
#include <GUIConstantsEx.au3>


Opt('GUIOnEventMode', 1)

Global $sTitel = 'Nearest Sunday'

Local $hMainGui = GUICreate($sTitel, 400, 280, 140, 150)
GUISetOnEvent($GUI_EVENT_CLOSE, '_MyExit')
Global $idMnuHELP = GUICtrlCreateMenu("?", -1, 1)
GUICtrlCreateMenuItem("Autor: autoBert", $idMnuHELP)
GUICtrlCreateMenuItem("e&xit", $idMnuHELP)
GUICtrlSetOnEvent(-1, '_MyExit')

Local $idEdtDATE = GUICtrlCreateDate("01.01.2009", 20, 10, 150, 25, $DTS_UPDOWN)
Local $DTM_SETFORMAT_ = 0x1032
Local $style = "dd.MM.yyyy"
GUICtrlSendMsg($idEdtDATE, $DTM_SETFORMAT_, 0, $style)
GUICtrlSetFont(-1, 12)

GUICtrlCreateButton('&Check nearest Sunday', 220, 10, 150, 25)
GUICtrlSetOnEvent(-1, '_CheckDate')

Local $idLblRESULT = GUICtrlCreateLabel("", 12, 50, 200,25)
GUICtrlCreateButton('e&xit', 220, 115, 150, 25)
GUICtrlSetOnEvent(-1, '_MyExit')

GUISetState()

While 1
Sleep(120)
WEnd

Func _MyExit()
GUIDelete($hMainGui)
Exit
EndFunc ;==>_MyExit

Func _CheckDate()
Local $sCheckDate = GUICtrlRead($idEdtDATE), $sMsg
Local $iDay = StringLeft($sCheckDate,2)
Local $iMonth = StringMid($sCheckDate,4,2)
Local $iYear = StringRight($sCheckDate,4)
ConsoleWrite($sCheckDate&@CRLF)
Local $iDayOfWeek = _DateToDayOfWeekISO($iYear,$iMonth,$iDay)
Switch $iDayOfWeek
Case 7    ;<=
$sMsg = "Lol, today is a sunday!"
case 1,2,3
$sMsg = "The nearest sunday is " & _DateAdd("D",$iDayOfWeek*-1,$iYear&"/"&$iMonth&"/"&$iDay)
Case Else
$sMsg = "The nearest sunday is " & _DateAdd("D",7-$iDayOfWeek,$iYear&"/"&$iMonth&"/"&$iDay)
EndSwitch
GUICtrlSetData($idLblRESULT, $sMsg)
GUISetState()
EndFunc ;==>_CallEaster

Edit: Script correctedt, thanks to Malkey

Edited by AutoBert
Posted

Your Welcome! You'll find the Help file very useful for the rich compliment of date manipulation functions.

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Posted

This example appears to be returning the results you are after in post #1.

; get closest Sunday to whatever date
#include <date.au3>
Local $sDate, $aDate, $sRet, $iDay
For $i = 1 To 14
    $sDate = "2013/01/" & StringRight("0" & $i, 2)
    ConsoleWrite($sDate & " ")
    $aDate = StringSplit($sDate, "/", 2)
    $iDay = _DateToDayOfWeek($aDate[0], $aDate[1], $aDate[2])
    ConsoleWrite(_DateDayOfWeek($iDay, 1) & "  ")
    Switch $iDay
        Case 1
            $sRet = $sDate & " (Today)"
        Case 2 To 4
            $sRet = _DateAdd("D", (1 - $iDay), $sDate) & " (Previous Sunday)"
        Case 5 To 7
            $sRet = _DateAdd("D", 8 - $iDay, $sDate) & " (Coming Sunday)"
    EndSwitch
    ConsoleWrite($sRet & @LF)
Next

#cs
Output @ console:-
2013/01/01 Tue  2012/12/30 (Previous Sunday)
2013/01/02 Wed  2012/12/30 (Previous Sunday)
2013/01/03 Thu  2013/01/06 (Coming Sunday)
2013/01/04 Fri  2013/01/06 (Coming Sunday)
2013/01/05 Sat  2013/01/06 (Coming Sunday)
2013/01/06 Sun  2013/01/06 (Today)
2013/01/07 Mon  2013/01/06 (Previous Sunday)
2013/01/08 Tue  2013/01/06 (Previous Sunday)
2013/01/09 Wed  2013/01/06 (Previous Sunday)
2013/01/10 Thu  2013/01/13 (Coming Sunday)
2013/01/11 Fri  2013/01/13 (Coming Sunday)
2013/01/12 Sat  2013/01/13 (Coming Sunday)
2013/01/13 Sun  2013/01/13 (Today)
2013/01/14 Mon  2013/01/13 (Previous Sunday)
#ce

@AutoBert

Your example is incorrectly calling 05/01/2013 a Sunday.

@kylomas

Your example works fine on the 2013/01/04.

Posted

malkey, thanks, I tested it with various variable values before switching it back to the @wday macro

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Posted (edited)

chakka,

After looking at malkey's code I realized that I had not accounted for runday = Sunday. Changed the code as follows

; get closest Sunday from whatever date this is run

#include <date.au3>

; added the following to account for runday = Sunday

if @wday = 1 then
    consolewrite(_nowcalcdate() & @lf)
    Exit
endif

if @wday <= 4 then
    local $Sunday = _dateadd('D',-(7-@wday),_NowCalcDate())
    ConsoleWrite($Sunday & @LF)
Else
    local $Sunday = _dateadd('D',(7-@wday)+1,_NowCalcDate())
    ConsoleWrite($Sunday & @LF)
endif

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Posted

This is better (adapting malkey's technique)

; get closest Sunday from whatever date this is run

#include <date.au3>

ConsoleWrite(_ClosestSunday() & @lf)

func _ClosestSunday()

    switch @WDAY
        case 1
            return _nowcalcdate()
        case 2 to 4
            return _dateadd('D',-(7-@wday),_NowCalcDate())
        case 5 to 7
            return _dateadd('D',(7-@wday)+1,_NowCalcDate())
    endswitch

endfunc

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Posted

Nice functions people.

Just an observation, but it's best to declare variables 'outside of blocks.'

Local $fVar
If True Then
    $fVar = True
Else
    $fVar = False
EndIf

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Posted (edited)

Here another method:

#include <Date.au3>
ConsoleWrite(NearestSunday("2013/01/01") & @LF)
ConsoleWrite(NearestSunday("2013/01/09") & @LF)
ConsoleWrite(NearestSunday("2013/01/20") & @LF)
ConsoleWrite(NearestSunday("2013/01/31") & @LF)


Func NearestSunday($sDate) ;format YYYY/MM/DD
    Local $aDate = StringSplit($sDate, "/", 2)
    Local $iD = _DateToDayOfWeek($aDate[0], $aDate[1], $aDate[2])
    Switch $iD
        Case 2 To 4
            Return _DateAdd("d", -$iD + 1, $sDate)
        Case 5 To 7
            Return _DateAdd("d", -$iD + 8, $sDate)
        Case Else
            Return $sDate
    EndSwitch
EndFunc

Edit: ok, it is very close to Malkey's version... :whistle:

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Posted

And before someone beats me to it...

The one-line version:

#include <date.au3>
MsgBox(0,"",_dateadd('D',-(@WDAY - 1 + (@WDAY > 4) * -7),_NowCalcDate()))

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...