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
copyraw
FormatTime( TimeString, Format )
{
     FormatTime, FormattedTime , TimeString, %Format% 
     return Formattedtime
}
  1.  FormatTime( TimeString, Format ) 
  2.  { 
  3.       FormatTime, FormattedTime , TimeString, %Format% 
  4.       return Formattedtime 
  5.  } 


The following function is to convert a given SQL date or a European date formatted value:
copyraw
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
  1.  FormatDate(val) 
  2.          { 
  3.              ; SQL format (yyyy-mm-dd HH:mm:ss) 
  4.              IfInString, val, - 
  5.              { 
  6.                  StringReplace, val, val, -,,All 
  7.                  StringReplace, val, val, :,,All 
  8.                  StringReplace, val, val, %A_Space%,,All 
  9.              } 
  10.              ; Standard European format (dd/mm/yyyy HH:mm:ss) 
  11.              IfInString, val, / 
  12.              { 
  13.                  ThisTime:=SubStr(val, InStr(val, " ")+1) 
  14.                  StringReplace, ThisTime, ThisTime, :,,
  15.                  ThisTime=%ThisTime% 
  16.                  ThisDate:=SubStr(val, 1, InStr(val, " ")) 
  17.                  ThisDate=%ThisDate% 
  18.                  ThisDate:=SubStr(ThisDate, 7) . SubStr(ThisDate, 4, 2) . SubStr(ThisDate, 1, 2) 
  19.                  ThisDate=%ThisDate% 
  20.                  val=%ThisDate%%ThisTime% 
  21.              } 
  22.              FormatTime, val, %val%, ddd dd-MMM-yyyy HH:mm:ss 
  23.              Return val 
  24.          } 
  25.   
  26.   
  27.          // 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.:
copyraw
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
    	}
  1.  FormatSeconds(val) 
  2.          { 
  3.              IfInString, val, - 
  4.              { 
  5.                  Month:=SubStr(val, 8, 3) 
  6.                  Month:=Month * 1 
  7.                  Months=Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec 
  8.                  StringSplit, MonthNameArray, Months, | 
  9.                  Month:=MonthNameArray%Month% 
  10.                  TimeString1:=SubStr(val, 12, 4) 
  11.                  TimeString1=%TimeString1%%Month% 
  12.                  TimeString1:=TimeString1 . SubStr(val, 5, 2) . SubStr(val, 17, 2) . SubStr(val, 20, 2) . SubStr(val, 23, 2) 
  13.                  TimeString2:=A_Now 
  14.                  EnvSub, TimeString2, %TimeString1%, Seconds 
  15.                  val:=TimeString2 
  16.              } 
  17.   
  18.              strVal= 
  19.              ThisDays:=val / 86400 
  20.              IfInString, ThisDays, . 
  21.                  ThisDays:=SubStr(ThisDays, 1, InStr(ThisDays, ".")-1) 
  22.              ThisHours:=(Mod(val, 86400)/3600) 
  23.              IfInString, ThisHours, . 
  24.                  ThisHours:=SubStr(ThisHours, 1, InStr(ThisHours, ".")-1) 
  25.              ThisMins:=(Mod(val, 3600)/60) 
  26.              IfInString, ThisMins, . 
  27.                  ThisMins:=SubStr(ThisMins, 1, InStr(ThisMins, ".")-1) 
  28.              ThisSecs:=Mod(val, 60) 
  29.              IfInString, ThisSecs, . 
  30.                  ThisSecs:=SubStr(ThisSecs, 1, InStr(ThisSecs, ".")-1) 
  31.              If ThisDays>0 
  32.              { 
  33.                  If ThisDays>365 
  34.                  { 
  35.                      ThisYears:=ThisDays/365 
  36.                      IfInString, ThisYears, . 
  37.                          ThisYears:=SubStr(ThisYears, 1, InStr(ThisYears, ".")-1) 
  38.   
  39.                      Grammar:=(ThisYears=1) ? "" : "s" 
  40.                      strVal=%ThisYears% year%Grammar%, 
  41.                      ThisDays:=ThisDays - (ThisYears*365) 
  42.                      Grammar:=(ThisDays=1) ? "" : "s" 
  43.                      strVal=%strVal% %ThisDays% day%Grammar%, 
  44.                  } else { 
  45.                      Grammar:=(ThisDays=1) ? "" : "s" 
  46.                      strVal=%ThisDays% day%Grammar%, 
  47.                  } 
  48.              } 
  49.              If ThisHours>0 
  50.              { 
  51.                  Grammar:=(ThisHours=1) ? "" : "s" 
  52.                  strVal=%strVal% %ThisHours% hour%Grammar%, 
  53.              } 
  54.              If ThisMins>0 
  55.              { 
  56.                  Grammar:=(ThisMins=1) ? "" : "s" 
  57.                  strVal=%strVal% %ThisMins% minute%Grammar%, 
  58.              } 
  59.              Grammar:=(ThisSecs=1) ? "" : "s" 
  60.              strVal=%strVal% %ThisSecs% second%Grammar% 
  61.              Return strVal 
  62.          } 
Category: AutoHotkey :: Article: 384

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: The information on this website is provided without warranty and any content is merely the opinion of the author. Please try to test in development environments prior to adapting them to your production environments. The articles are written in good faith and, at the time of print, are working examples used in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin - Valid till 8 May 2022 3QnhmaBX7LQSRsC9hh6Je9rGQKEGNQNfPb
© 2021 Joel Lipman .com. All Rights Reserved.