Jump to content

How to get previous date from a string


ur
 Share

Recommended Posts

I have date in the string format as "DD-MM-YYYY".

 

I need to get yesterday's date from it.

I tried converting this from _DateTimeFormat but not working.

Is there any direct UDF available to get this.?

Link to comment
Share on other sites

Oh!! Thanks @jguinch

Unnecessarily, I created below logic in the meanwhile.

Func yeterdayDate($sDate)

$f =  StringSplit($sDate,"-");StringSplit("22-01-2018","-")
_ArrayDisplay($f)
if $f[1]>1 then
    $f[1]-= 1
Else
    If Number($f[2])-1 =1 or Number($f[2])=3 or Number($f[2])=5 or Number($f[2])=7 or Number($f[2])=8 or Number($f[2])=10 or Number($f[2])=12 Then
        $f[1]=31
    ElseIf Number($f[2] - 1) < 1 Then
        $f[1]=31
        $f[2]=12
        $f[3]=Number($f[3])-1
    Else
        $f[1]=30
    EndIf
EndIf

padZero($f[1])
padZero($f[2])
padZero($f[3])

;MsgBox(0,"",$f[1]&"-"&$f[2]&"-"&$f[3])
return $f[1]&"-"&$f[2]&"-"&$f[3]

EndFunc

Func padZero(ByRef $sValue)
    if Number($sValue)<10 Then
        $sValue = "0"&Number($sValue)
    EndIf
EndFunc

 

Link to comment
Share on other sites

#Include <Date.au3>

Local $sDate = "01-01-2018"
Local $sDatePreviousDay = _DateAdd("D", -1,  StringRegExpReplace($sDate, "(\d{2})[/-](\d{2})[/-](\d{4})", "$3/$2/$1") )
ConsoleWrite($sDatePreviousDay)

;)

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

×
×
  • Create New...