Jump to content

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

 

Link to comment
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.

Your code:
 

#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.

Link to comment
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

 

Link to comment
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.

Link to comment
Share on other sites

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

Link to comment
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.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
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)

Link to comment
Share on other sites

Link to comment
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.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
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)

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

  • Recently Browsing   0 members

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