# Converting AM/PM time to 24-hour

Go to solution Solved by rsn,

## Recommended Posts

Here's my code:

\$tin = "8:00 PM"
\$vec = StringSplit(\$tin,": ",0)    ; STR_CHRSPLIT
\$hr = Number(\$vec[1])
\$min = \$vec[2]
If \$vec[3]="PM" Then
\$hr = \$hr + 12
EndIf
\$ret = \$hr + ":" + \$min
MsgBox(0,"",\$ret,15)

\$ret should be 20:00 but it is actually 20

Ideas? I have tried applying String()

Spoiler

CDebug Dumps values of variables including arrays and DLL structs, to a GUI, to the Console, and to the Clipboard

##### Share on other sites

• Solution

Change

`\$ret = \$hr + ":" + \$min`

to

`\$ret = \$hr & ":" & \$min`

##### Share on other sites

• Moderators
23 minutes ago, c.haslam said:

Here's my code:

\$tin = "8:00 PM"
\$vec = StringSplit(\$tin,": ",0)    ; STR_CHRSPLIT
\$hr = Number(\$vec[1])
\$min = \$vec[2]
If \$vec[3]="PM" Then
\$hr = \$hr + 12
EndIf
\$ret = \$hr + ":" + \$min
MsgBox(0,"",\$ret,15)

\$ret should be 20:00 but it is actually 20

Ideas? I have tried applying String()

I'd suggest you actually run that code, there's no \$vec[3] so it will certainly crash.  As RSN stated, string concatenation is done with an ampersand -> &, we use the + operator only for math.

If your strings will look like what you have (eg. "8:00 PM"), then maybe try a regex.

```#include <Array.au3>
Global \$gaArgs = StringSplit("8:00 PM", ":")
_ArrayDisplay(\$gaArgs)```

Simple approach (keep in mind, the arrays for regular expressions are zero based)

```#include <Array.au3>
Global \$gsTime = "8:00 PM"
Global \$gaArgs = StringRegExp(\$gsTime, "(.+?)(?:\W|\$)", 3)
_ArrayDisplay(\$gaArgs)```

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

##### Share on other sites

Thank you both. I have been away from AutoIt for too long, so I tend to think PerfectScript.

Spoiler

CDebug Dumps values of variables including arrays and DLL structs, to a GUI, to the Console, and to the Clipboard

##### Share on other sites

@SmOke_N OP's StringSplit does have two delimiters (":" and " ") so \$vec[3] is defined. Your explanation is certainly better than mine 🤦‍♂️

##### Share on other sites

• Moderators
5 minutes ago, rsn said:

@SmOke_N OP's StringSplit does have two delimiters (":" and " ") so \$vec[3] is defined. Your explanation is certainly better than mine 🤦‍♂️

Damn these eyes.. I seriously didn't see the space!

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

##### Share on other sites

Posted (edited)

Edited by UEZ

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

##### Share on other sites

Or m23 :

“They did not know it was impossible, so they did it” ― Mark Twain

Spoiler

##### Share on other sites

You can also use a one-liner:

```Local \$a = ["  12:0am", "12:12 AM", "2:3   aM", "11:59 am", "12:00 pm", "12:45pm ", "03:15 PM", "11:59 Pm"], \$t
For \$s In \$a
\$t = Execute((StringRegExpReplace(\$s, "(?i)(\d{1,2}):(\d{1,2})\h*([AP])M", "StringFormat('%02i', Mod(Execute('((  ((''\$3'' & ''\$1'' = ''A12'') OR (''\$3'' = ''P'' AND ''\$1'' < 12)) ? 12 : 0) + \$1)'), 24)) & ':' & StringFormat('%02i', '\$2')")))
ConsoleWrite(\$s & @TAB & \$t & @LF)
Next```

Beware that the OP code (even when fixed) gets 12 AM and 12 PM wrong!

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

##### Share on other sites

Posted (edited)

Oh well, another one liner then (less abstruse )  :

`\$t = Mod(StringRegExp(\$s, "(\d{1,2})", 1)[0], 12) + (StringRegExp(\$s, "(?i).*([AP]M)", 1)[0] = "AM" ? 0 : 12) & StringRegExp(\$s, "(:\d{1,2})", 1)[0]`

Edited by Nine

“They did not know it was impossible, so they did it” ― Mark Twain

Spoiler

##### Share on other sites

Ah yes, HH mod 12 is smarter.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...