Wednesday, May 16, 2012

AutoHotkey Format Date and Format Seconds

As you can tell these are my messed up functions that convert dates into seconds and vice-versa. They're a little disorganised but they're the ones I copy and paste to my scripts then modify.

In it's straightforward form
FormatTime( TimeString, Format )
{
     FormatTime, FormattedTime , TimeString, %Format% 
     return Formattedtime
}


The following function is to convert a given SQL date or a European date formatted value:
    	FormatDate(val)
    	{
    		; SQL format (yyyy-mm-dd HH:mm:ss)
    		IfInString, val, -
    		{
    			StringReplace, val, val, -,,All
    			StringReplace, val, val, :,,All
    			StringReplace, val, val, %A_Space%,,All
    		}
    		; Standard European format (dd/mm/yyyy HH:mm:ss)
    		IfInString, val, /
    		{
    			ThisTime:=SubStr(val, InStr(val, " ")+1)
    			StringReplace, ThisTime, ThisTime, :,,A
    			ThisTime=%ThisTime%
    			ThisDate:=SubStr(val, 1, InStr(val, " "))
    			ThisDate=%ThisDate%
    			ThisDate:=SubStr(ThisDate, 7) . SubStr(ThisDate, 4, 2) . SubStr(ThisDate, 1, 2)
    			ThisDate=%ThisDate%
    			val=%ThisDate%%ThisTime%
    		}
    		FormatTime, val, %val%, ddd dd-MMM-yyyy HH:mm:ss
    		Return val
    	}


    	// returns format Sat 01-Jan-2011 01:00:00

The next function usually accompanies the above one. It will basically convert seconds into days, hours, minutes, etc.:

    	FormatSeconds(val)
    	{
    		IfInString, val, -
    		{
    			Month:=SubStr(val, 8, 3)
    			Month:=Month * 1
    			Months=Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec
    			StringSplit, MonthNameArray, Months, |
    			Month:=MonthNameArray%Month%
    			TimeString1:=SubStr(val, 12, 4)
    			TimeString1=%TimeString1%%Month%
    			TimeString1:=TimeString1 . SubStr(val, 5, 2) . SubStr(val, 17, 2) . SubStr(val, 20, 2) . SubStr(val, 23, 2)
    			TimeString2:=A_Now
    			EnvSub, TimeString2, %TimeString1%, Seconds
    			val:=TimeString2
    		}

    		strVal=
    		ThisDays:=val / 86400
    		IfInString, ThisDays, .
    			ThisDays:=SubStr(ThisDays, 1, InStr(ThisDays, ".")-1)
    		ThisHours:=(Mod(val, 86400)/3600)
    		IfInString, ThisHours, .
    			ThisHours:=SubStr(ThisHours, 1, InStr(ThisHours, ".")-1)
    		ThisMins:=(Mod(val, 3600)/60)
    		IfInString, ThisMins, .
    			ThisMins:=SubStr(ThisMins, 1, InStr(ThisMins, ".")-1)
    		ThisSecs:=Mod(val, 60)
    		IfInString, ThisSecs, .
    			ThisSecs:=SubStr(ThisSecs, 1, InStr(ThisSecs, ".")-1)
    		If ThisDays>0
    		{
    			If ThisDays>365
    			{
    				ThisYears:=ThisDays/365
    				IfInString, ThisYears, .
    					ThisYears:=SubStr(ThisYears, 1, InStr(ThisYears, ".")-1)

                    Grammar:=(ThisYears=1) ? "" : "s"
    				strVal=%ThisYears% year%Grammar%,
    				ThisDays:=ThisDays - (ThisYears*365)
                    Grammar:=(ThisDays=1) ? "" : "s"
    				strVal=%strVal% %ThisDays% day%Grammar%,
    			} else {
                    Grammar:=(ThisDays=1) ? "" : "s"
    				strVal=%ThisDays% day%Grammar%,
    			}
    		}
    		If ThisHours>0
    		{
                Grammar:=(ThisHours=1) ? "" : "s"
    			strVal=%strVal% %ThisHours% hour%Grammar%,
    		}
    		If ThisMins>0
    		{
                Grammar:=(ThisMins=1) ? "" : "s"
    			strVal=%strVal% %ThisMins% minute%Grammar%,
    		}
            Grammar:=(ThisSecs=1) ? "" : "s"
    		strVal=%strVal% %ThisSecs% second%Grammar%
    		Return strVal
    	}

Add comment

Please note: all comments are reviewed before being published.


Security code
Refresh

Member Login

Joes Latest Members

A huge WELCOME goes to today's newest members:

  • infonat
Member Signups (Activated)
BeforeCurrentTrend
Day21=
Week105ê
Month11224ê
Year2,265612ê

Member Stats
68 guests are currently online.
1,178 members are still deciding.
There are 5,935 members in total.

Latest Comments

Paypal Donations

Want to support my work? Any donation is a blessing :c)