Jump to content

autoit calculates date


Recommended Posts

  • Moderators

Loc,

If you want any number of days to be displayed as "months" of 30 day periods and then the remainder, you need to use Int and Mod - like this:

$iPeriod = 38
$iMonthLength = 30

$iMon = Int($iPeriod / $iMonthLength)
$iDays = Mod($iPeriod, $iMonthLength)

ConsoleWrite($iMon & ":" & $iDays & @CRLF)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

8 hours ago, FrancescoDiMuro said:

Don't tell February or it gets mad :D

P.S.: post what you've tried :)

My point is just an example for me to follow. from 8/4/2021 - 8/6/2021 is 60 days instead of showing 60 days I want to show 1 month and 30 days past today it is 2 months 0 days and every day like that, the day part will be added 1 until the end of the 30th it will be 1 month and the day will be 0

Link to comment
Share on other sites

This method has more accuracy than a 30 days a month method.

#include <Date.au3>

Local $sStartDate = "8/4/2021" ; Format is "D/M/YYYY", not US date format.
Local $sEndDate   = "9/6/2021" ; Format is "D/M/YYYY", not US date format.

Local $sStart = _DateConvert($sStartDate, 0)
Local $sEnd   = _DateConvert($sEndDate, 0)
ConsoleWrite("$sStart  = " & $sStart & ' in format "YYYY/MM/DD"' & @CRLF)
ConsoleWrite("$sEnd    = " & $sEnd & ' in format "YYYY/MM/DD"' & @CRLF)

Local $iNumMnths       = _DateDiff('M', $sStart, $sEnd)          ; Number of months between dates.
Local $sStartPlusMnths = _DateAdd("M", $iNumMnths, $sStart)      ; $sStartDatePlusMnths = Start Date plus number of months between dates.
Local $iRemDays        = _DateDiff('D', $sStartPlusMnths, $sEnd) ; Number of remaining days.
Local $iTotalDays      = _DateDiff('D', $sStart, $sEnd)          ; Total number of days between dates.

ConsoleWrite("(" & $iNumMnths & " month" & ($iNumMnths = 1 ? " " : "s ") & _
             $iRemDays & " day" & ($iRemDays = 1 ? ")" : "s)") & " or " & _
             $iTotalDays & " days" & @CRLF)

; ============== _DateConvert ================= 
; If $US = 1 then format of $Date is in "[M]M/[D]D/YYYY" otherwise,
; If $US = 0 then format of $Date is in "[D]D/[M]M/YYYY" (not US date format)
; The converted Date out has format "YYYY/MM/DD" (for use in _Date...() AutoIt UDFs)
;
Func _DateConvert($Date, $US = 1)
    Local $aTemp = StringSplit($Date, "/")
    Return StringFormat("%4i/%02i/%02i", $aTemp[3], ($US ? $aTemp[1] : $aTemp[2]), ($US ? $aTemp[2] : $aTemp[1]))
EndFunc   ;==>_DateConvert


#cs ; Returns:-
$sStart  = 2021/04/08 in format "YYYY/MM/DD"
$sEnd    = 2021/06/09 in format "YYYY/MM/DD"
(2 months 1 day) or 62 days
#ce ; Returns:-

 

Link to comment
Share on other sites

this _ElapsedTime() function returns the elapsed time between 2 dates in a 3 elements array expressed in years, months, days (no error checking implemented)

#include <Date.au3>

Local $Start = "1962/05/02" ; start date
Local $Stop = _NowCalcDate()  ; end date

Local $aElapsed = _ElapsedTime($Start, $Stop)

MsgBox(0, "Elapsed Time", $aElapsed[0] & " years, " & $aElapsed[1] & " months, " & $aElapsed[2] & " days")

; returns the elapsed time between 2 dates expressed in years, months, days
Func _ElapsedTime($sStartDate, $sEndDate)
    Local Enum $iYears, $iMonths, $iDays
    Local $aResult[3] ;
    $aResult[$iYears] = _DateDiff('Y', $sStartDate, $sEndDate)
    $aResult[$iMonths] = _DateDiff('M', _DateAdd('Y', $aResult[$iYears], $sStartDate), $sEndDate)
    $aResult[$iDays] = _DateDiff('D', _DateAdd('M', $aResult[$iMonths], _DateAdd('Y', $aResult[$iYears], $sStartDate)), $sEndDate)
    Return $aResult ; [0] years; [1] months; [2] days
EndFunc   ;==>_ElapsedTime

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

13 hours ago, Chimp said:

Hàm _ElapsedTime () này trả về thời gian đã trôi qua giữa 2 ngày trong mảng 3 phần tử được biểu thị bằng năm, tháng, ngày (không thực hiện kiểm tra lỗi)

#include <Date.au3>

 $ Start  địa phương =  "1962/05/02"  ; ngày bắt đầu 
Local  $ Stop  =  _NowCalcDate ( )   ; ngày cuối

Local  $ aElapsed  =  _ ElapsedTime ( $ Start ,  $ Stop )

MsgBox ( 0 ,  "Thời gian đã trôi qua" ,  $ aElapsed [ 0 ]  &  "years",  &  $ aElapsed [ 1 ]  &  "months"  &  $ aElapsed [ 2 ]  &  "days" )

; trả về thời gian đã trôi qua giữa 2 ngày được biểu thị bằng năm, tháng, ngày 
Func  _ ElapsedTime ( $ sStartDate ,  $ sEndDate ) 
    Local  Enum  $ iYears ,  $ iMonths ,  $ iDays 
    Local  $ aResult [ 3 ]  ; 
    $ aResult [ $ iYears ]  =  _DateDiff ( 'Y' ,  $ sStartDate ,  $ sEndDate ) 
    $ aResult [ $ iMonths ]  =  _DateDiff ( 'M' ,  _DateAdd ('Y' ,  $ aResult [ $ iYears ] ,  $ sStartDate ) ,  $ sEndDate ) 
    $ aResult [ $ iDays ]  =  _DateDiff ( 'D' ,  _DateAdd ( 'M' ,  $ aResult [ $ iMonths ] ,  _DateAdd ( 'Y' ,  $ aResult [ $ iYears ] ,  $ sStartDate ) ) ,  $ sEndDate ) 
    Trả về  $ aResult  ; [0] năm; [1 tháng; [2] ngày 
EndFunc   ; ==> _ ElapsedTime

 

Thanks for the above suggestions. I usually use the date format Day Months Year

Link to comment
Share on other sites

#include <String.au3>

ConsoleWrite(_FormatDate('25/12/2020'))

Func _FormatDate($sDate)
    Local $aDate = StringSplit($sDate, '/', $STR_NOCOUNT)
    If @ERROR Then
        Return -1
    EndIf
    If UBound($aDate) = 3 Then
        Return StringFormat('%04s/%02s/%02s', $aDate[2], $aDate[1], $aDate[0])
    EndIf
    Return -1
EndFunc

Output:

Quote

2020/12/25

Combined with @Chimp's function:

#include <String.au3>
#include <Date.au3>

;ConsoleWrite(_FormatDate('25/12/2020'))

Local $Start = _FormatDate("02/05/1962") ; start date *dd/mm/yyyy
Local $Stop = _NowCalcDate()  ; end date

Local $aElapsed = _ElapsedTime($Start, $Stop)

;~ MsgBox(0, "Elapsed Time", $aElapsed[0] & " years, " & $aElapsed[1] & " months, " & $aElapsed[2] & " days") ; Years, Months, Days
MsgBox(0, "Elapsed Time", $aElapsed[2] & " days, " & $aElapsed[1] & " months, " & $aElapsed[0] & " years") ; Days, Months, Years

; returns the elapsed time between 2 dates expressed in years, months, days
Func _ElapsedTime($sStartDate, $sEndDate)
    Local Enum $iYears, $iMonths, $iDays
    Local $aResult[3] ;
    $aResult[$iYears] = _DateDiff('Y', $sStartDate, $sEndDate)
    $aResult[$iMonths] = _DateDiff('M', _DateAdd('Y', $aResult[$iYears], $sStartDate), $sEndDate)
    $aResult[$iDays] = _DateDiff('D', _DateAdd('M', $aResult[$iMonths], _DateAdd('Y', $aResult[$iYears], $sStartDate)), $sEndDate)
    Return $aResult ; [0] years; [1] months; [2] days
EndFunc   ;==>_ElapsedTime

Func _FormatDate($sDate)
    Local $aDate = StringSplit($sDate, '/', $STR_NOCOUNT)
    If @ERROR Then
        Return -1
    EndIf
    If UBound($aDate) = 3 Then
        Return StringFormat('%04s/%02s/%02s', $aDate[2], $aDate[1], $aDate[0])
    EndIf
    Return -1
EndFunc

 

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...