Jump to content

Recommended Posts

Posted

  On 11/21/2011 at 6:31 PM, 'JohnOne said:

_DateDiff()

I wrote "in the same format".

With _DateDiff I can only get the difference in hour, mins, secs, etc.

Posted (edited)

Plus more ...

  Quote

D = Difference in days between the given dates

M = Difference in months between the given dates

Y = Difference in years between the given dates

w = Difference in Weeks between the given dates

h = Difference in hours between the given dates

n = Difference in minutes between the given dates

s = Difference in seconds between the given dates

But what do you exactly mean by same format? Could you provide an example of what you're intending your output to be? Because one can only presume that if you want to find the difference between two dates then _DateDiff would be the obvious choice. Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

Use _DateDiff and then format the result to your needs.

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

Something like that?

#include <Date.au3>

MsgBox(0, "Test",  Sec_2_Time_Format(_DateDiff("s", "2011/01/01 00:00:00", _NowCalc())))

Func Sec_2_Time_Format($iSec) ;coded by UEZ
    Local $days = 0
    Local $sec = Mod($iSec, 60)
    Local $min = Mod(Int($iSec / 60), 60)
    Local $hr = Int($iSec / 60 ^ 2)
    If $hr > 23 Then
        $days = Floor($hr / 24)
        $hr -= $days * 24
    EndIf
    Return StringFormat("%01id %02i:%02i:%02i", $days, $hr, $min, $sec)
EndFunc   ;==>Sec_2_Time_Format

Br,

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

Example:

Date/Time = 2009/10/21 12:00

Current Date/Time = 2011/11/25 12:00

Difference = 2 years, 1 month, 4 days, 0 hours, 0 minutes

UEZ, you got the point but I don't understand why $days = 0. Plus, years and months are missing.

It's a pity that AutoIT doesn't have a function to do this with a simple string...

Posted (edited)

OK, your 'same format' was a little obscure, I would've worded it as '...output to ## Years ## Months ## Days ## Hours ## Minutes.'

Take a look at WinAPIEx.au3 by Yashied and look for the function _WinAPI_StrFromTimeInterval() for something 'inbuilt' into the Windows API.

Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

  • Moderators
Posted (edited)

FSoft,

  Quote

It's a pity that AutoIT doesn't have a function to do this with a simple string

Why should AutoIt have a builtin function when it is trivial to to this in a function you can write in a few seconds? :D

I take it this what you were looking for? :rip:

#include <date.au3>

$sEndDate = "2011/11/25 12:00"
$sStartDate = "2009/10/21 12:00"

$sDifference = _Date_Difference($sStartDate, $sEndDate)
MsgBox(0, "Difference", $sDifference)

Func _Date_Difference($sStartDate, $sEndDate)

    Local $sReturn = ""

    $iYears = _DateDiff("Y", $sStartDate, $sEndDate)
    $sReturn &= $iYears & "/"

    ; We now alter the start date to add the Years we have just found
    $sStartDate = _DateAdd("Y", $iYears, $sStartDate)
    ; And now we look for the Months
    $iMonths = _DateDiff("M", $sStartDate, $sEndDate)
    $sReturn &= $iMonths & "/"

    ; Now we add the Months
    $sStartDate = _DateAdd("M", $iMonths, $sStartDate)
    ; And look for the Days
    $iDays = _DateDiff("D", $sStartDate, $sEndDate)
    $sReturn &= $iDays & " "

    ; I am sure you get the idea......
    $sStartDate = _DateAdd("D", $iDays, $sStartDate)
    $iHours = _DateDiff("h", $sStartDate, $sEndDate)
    $sReturn &= $iHours & ":"

    $sStartDate = _DateAdd("h", $iHours, $sStartDate)
    $iMins = _DateDiff("n", $sStartDate, $sEndDate)
    $sReturn &= $iMins & ":"

    $sStartDate = _DateAdd("n", $iHours, $sStartDate)
    $iSecs = _DateDiff("s", $sStartDate, $sEndDate)
    $sReturn &= $iSecs

    Return $sReturn

EndFunc

All clear? :oops:

M23

Edit: Small typo.

Edited by Melba23

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:

  Reveal hidden contents

 

Posted

Thought using EPOCH would be easier... well :D...

$iTicks_Now = _Epoch_encrypt(@YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC)
$iTicks_Than = _Epoch_encrypt(2009 & "/" & 10 & "/" & 21 & " " & 12 & ":" & 00 & ":" & 00)
ConsoleWrite($iTicks_Now - $iTicks_Than & @TAB & $iTicks_Now & @TAB & $iTicks_Than & @CRLF)
$iTicksDif_tmp = _EPOCH_decrypt($iTicks_Now - $iTicks_Than)
$aTicksDif_tmp = StringSplit($iTicksDif_tmp, "/")
$aTicksDif_tmp[1] = StringFormat("%.2d",$aTicksDif_tmp[1]-1970)
$aTicksDif_tmp[2] = StringFormat("%.2d",$aTicksDif_tmp[2]-1)
$aTicksDif_tmp[3] = StringFormat("%.2d",Int(StringLeft($aTicksDif_tmp[3],2))-1)& StringTrimLeft($aTicksDif_tmp[3],2)
$iTicks_Dif = $aTicksDif_tmp[1] & "/" & $aTicksDif_tmp[2] & "/" & $aTicksDif_tmp[3]
MsgBox(0,"",$iTicks_Dif)
Func _EPOCH_decrypt($iEpochTime)
 ; [url="http://www.autoitscript.com/forum/index.php?showtopic=83667&hl=epoch"]http://www.autoitscript.com/forum/index.php?showtopic=83667&hl=epoch[/url]
 ; trancexx
 Local $iDayToAdd = Int($iEpochTime / 86400)
 Local $iTimeVal = Mod($iEpochTime, 86400)
 If $iTimeVal < 0 Then
  $iDayToAdd -= 1
  $iTimeVal += 86400
 EndIf
 Local $i_wFactor = Int((573371.75 + $iDayToAdd) / 36524.25)
 Local $i_xFactor = Int($i_wFactor / 4)
 Local $i_bFactor = 2442113 + $iDayToAdd + $i_wFactor - $i_xFactor
 Local $i_cFactor = Int(($i_bFactor - 122.1) / 365.25)
 Local $i_dFactor = Int(365.25 * $i_cFactor)
 Local $i_eFactor = Int(($i_bFactor - $i_dFactor) / 30.6001)
 Local $aDatePart[3]
 $aDatePart[2] = $i_bFactor - $i_dFactor - Int(30.6001 * $i_eFactor)
 $aDatePart[1] = $i_eFactor - 1 - 12 * ($i_eFactor - 2 > 11)
 $aDatePart[0] = $i_cFactor - 4716 + ($aDatePart[1] < 3)
 Local $aTimePart[3]
 $aTimePart[0] = Int($iTimeVal / 3600)
 $iTimeVal = Mod($iTimeVal, 3600)
 $aTimePart[1] = Int($iTimeVal / 60)
 $aTimePart[2] = Mod($iTimeVal, 60)
 Return SetError(0, 0, StringFormat("%.2d/%.2d/%.2d %.2d:%.2d:%.2d", $aDatePart[0], $aDatePart[1], $aDatePart[2], $aTimePart[0], $aTimePart[1], $aTimePart[2]))
EndFunc   ;==>_EPOCH_decrypt
Func _Epoch_encrypt($date)
 Local $main_split = StringSplit($date, " ")
 If $main_split[0] - 2 Then
  Return SetError(1, 0, "") ; invalid time format
 EndIf
 Local $asDatePart = StringSplit($main_split[1], "/")
 Local $asTimePart = StringSplit($main_split[2], ":")
 If $asDatePart[0] - 3 Or $asTimePart[0] - 3 Then
  Return SetError(1, 0, "") ; invalid time format
 EndIf
 If $asDatePart[2] < 3 Then
  $asDatePart[2] += 12
  $asDatePart[1] -= 1
 EndIf
 Local $i_aFactor = Int($asDatePart[1] / 100)
 Local $i_bFactor = Int($i_aFactor / 4)
 Local $i_cFactor = 2 - $i_aFactor + $i_bFactor
 Local $i_eFactor = Int(1461 * ($asDatePart[1] + 4716) / 4)
 Local $i_fFactor = Int(153 * ($asDatePart[2] + 1) / 5)
 Local $aDaysDiff = $i_cFactor + $asDatePart[3] + $i_eFactor + $i_fFactor - 2442112
 Local $iTimeDiff = $asTimePart[1] * 3600 + $asTimePart[2] * 60 + $asTimePart[3]
 Return SetError(0, 0, $aDaysDiff * 86400 + $iTimeDiff)
EndFunc   ;==>_Epoch_encrypt
  • Moderators
Posted

I think this is about as good as I can get - or am prepared to go!: :D

#include <Date.au3>

$sEndDate = "2011/11/25 12:00"
$sStartDate = "2009/10/21 12:00"

$sDifference = _Date_Difference($sStartDate, $sEndDate)
MsgBox(0, "Difference", $sDifference)

Func _Date_Difference($sStartDate, $sEndDate)

    Local $aUnit[6] = ["Y", "M", "D", "h", "n", "s"]
    Local $aInter[6] = ["/", "/", " ", ":", ":", ""]
    Local $sReturn = "", $iUnit

    For $i = 0 To 5
        $iUnit = _DateDiff($aUnit[$i], $sStartDate, $sEndDate)
        $sReturn &= $iUnit & $aInter[$i]
        $sStartDate = _DateAdd($aUnit[$i], $iUnit, $sStartDate)
    Next

    Return $sReturn

EndFunc

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:

  Reveal hidden contents

 

Posted (edited)

Here my extended version:

#include <date.au3>

MsgBox(0, "Test",  Sec_2_Time_Format(_DateDiff("s", "2011/01/01 00:00", _NowCalc())))

Func Sec_2_Time_Format($iSec) ;coded by UEZ
    Local $years, $months, $days
    Local $sec = Mod($iSec, 60)
    Local $min = Mod(Int($iSec / 60), 60)
    Local $hr = Int($iSec / 60 ^ 2)
    If $hr > 23 Then
        $days = Floor($hr / 24)
        $hr -= $days * 24
    EndIf
    $years = Floor($days / 365)
    $days -= ($years * 365)
    $months = Floor($days / 30.42)
    $days -= Floor($months * 30.42)
    Return StringFormat("%02i years, %02i month, %02i days, %02i minutes, %02i hours, %02i seconds", $years, $months, $days, $hr, $min, $sec) ;y,m,d,h,m,s
EndFunc   ;==>Sec_2_Time_Format

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

  • 2 weeks later...
Posted

Oops, still a question.

With StringFormat is possible to omit the zeroed items?

E.g. if it's 0years, 1months, 0days it's possible to display only 1months?

  • Moderators
Posted

FSoft,

Just check you have a return from _DateDiff like this: :oops:

#include <Date.au3>

$sEndDate = "2011/11/25 13:00"
$sStartDate = "2009/11/21 12:10"

$sDifference = _Date_Difference($sStartDate, $sEndDate)
MsgBox(0, "Difference", $sDifference)

Func _Date_Difference($sStartDate, $sEndDate)

    Local $aUnit[6] = ["Y", "M", "D", "h", "n", "s"]
    Local $aType[6] = ["years", "months", "days", "hours", "minutes", "seconds"]
    Local $sReturn = "", $iUnit

    For $i = 0 To 5
        $iUnit = _DateDiff($aUnit[$i], $sStartDate, $sEndDate)
        If $iUnit <> 0 Then
            $sReturn &= $iUnit & " " & $aType[$i] & " "
        EndIf
        $sStartDate = _DateAdd($aUnit[$i], $iUnit, $sStartDate)
    Next

    Return $sReturn

EndFunc

Is that it? :D

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:

  Reveal hidden contents

 

Posted

FSoft,

I think your finger slipped a couple of keys to the left.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

  • Moderators
Posted

FSoft,

Pay no attention to those two. I am glad I could help. :D

B23

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:

  Reveal hidden contents

 

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
  • Recently Browsing   0 members

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