Sign in to follow this  
Followers 0
ken82m

Convert Date Formats From String [Solved]

17 posts in this topic

#1 ·  Posted (edited)

All the time time functions want: YYYY/MM/DD HH:MM:SS

But the program I'm getting the info from will only return MM:DD:YYYY HH:MM:SS PM

Is their a function to convert it without 20 million string commands? lol :P

Thanks,

Kenny

Edited by ken82m

My Contributions _StringMultiReplace PC Builders Console - Secure PDF Creator - Cisco VPN Installer MS DNS Server Backup Script - MS DHCP Backup Script IT Admin Console - Toggle Admin Mode - MyMovies-Add Discs Script - IT Help Desk and System Information Tool - Set On Lid Close Power Option - Streaming Media Server & Website "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Share this post


Link to post
Share on other sites



All the time time functions want: YYYY/MM/DD HH:MM:SS

But the program I'm getting the info from will only return MM:DD:YYYY HH:MM:SS PM

Is their a function to convert it without 20 million string commands? lol :P

Thanks,

Kenny

Time to learn regular expressions.

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 this post


Link to post
Share on other sites

is kinda easy... start splitting the date with " : " as pattern then u can get the needed format date

Share this post


Link to post
Share on other sites

That's what I figured, haven't played with those yet. Wish me luck lol :P


My Contributions _StringMultiReplace PC Builders Console - Secure PDF Creator - Cisco VPN Installer MS DNS Server Backup Script - MS DHCP Backup Script IT Admin Console - Toggle Admin Mode - MyMovies-Add Discs Script - IT Help Desk and System Information Tool - Set On Lid Close Power Option - Streaming Media Server & Website "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Share this post


Link to post
Share on other sites

good luck

Share this post


Link to post
Share on other sites

;Convert MM:DD:YYYY HH:MM:SS PM to YYYY/MM/DD HH:MM:SS
$original = "04:30:1980 21:55:30"
$new = StringRegExpReplace($original, "\A(\d*):(\d*):(\d*)","$3/$1/$2")
MsgBox(0,"","Before: " & $original & @CRLF & "After: " & $new)

Share this post


Link to post
Share on other sites

Thanks guys. The StringRegExp gave me a headache so I did a stringsplit lol

I'll study that example you posted though and hopefully learn something.

It looks kinda sloppy I guess but it works lol

-Kenny

#include <array.au3>

$LogonTime = "10/18/2008 5:32 AM"

$Array = StringSplit($LogonTime, ":/ ")

If $Array[6] = "PM" Then
    $Array[4] = $Array[4] + 12
    $Array[6] = 30
EndIf

If $Array[6] = "AM" AND StringLen($Array[4]) = 1 Then
    $Array[4] = 0 & $Array[4]
    $Array[6] = 30
EndIf

$LogonTime = $Array[3] & "/" & $Array[1] & "/" & $Array[2] & " " & $Array[4] & ":" & $Array[5] & ":" & $Array[6]

MsgBox(4096,"", $LogonTime)

My Contributions _StringMultiReplace PC Builders Console - Secure PDF Creator - Cisco VPN Installer MS DNS Server Backup Script - MS DHCP Backup Script IT Admin Console - Toggle Admin Mode - MyMovies-Add Discs Script - IT Help Desk and System Information Tool - Set On Lid Close Power Option - Streaming Media Server & Website "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Share this post


Link to post
Share on other sites

Sometimes i need to convert to different formats, so this function helps me a lot (i just set required format in last parameter):

$sDateTime = "10:19:2008 03:13:55 AM"
$sConverted_DateTime = _DateTimeConvert($sDateTime, 0, "MM:DD:YYYY HH:MM:SS", "YYYY/MM/DD HH:MM:SS")

MsgBox(64, "_DateTimeConvert", $sDateTime & @CRLF & $sConverted_DateTime)

Func _DateTimeConvert($sDateTime, $iConvertAmPm=0, $sDateTimeFormat="DD/MM/YYYY HH:MM:SS", $sRetFormat="YYYY/MM/DD HH:MM:SS")
    Local $sAM_PM = StringStripWS(StringRegExpReplace($sDateTime, "\d+|:|/", ""), 4)
    
    Local $sDay = StringMid($sDateTime, StringInStr($sDateTimeFormat, "DD", 0, 1), 2)
    Local $sMonth = StringMid($sDateTime, StringInStr($sDateTimeFormat, "MM", 0, 1), 2)
    Local $sYear = StringMid($sDateTime, StringInStr($sDateTimeFormat, "YYYY", 0, 1), 4)
    
    Local $sHour = StringMid($sDateTime, StringInStr($sDateTimeFormat, "HH", 0, 1), 2)
    Local $sMinute = StringMid($sDateTime, StringInStr($sDateTimeFormat, "MM", 0, 2), 2)
    Local $sSecond = StringMid($sDateTime, StringInStr($sDateTimeFormat, "SS", 0, 1), 2)
    
    If $iConvertAmPm And StringStripWS($sAM_PM, 8) = "PM" Then
        If $sHour < 12 Then $sHour += 12
        If $sHour = 12 Then $sHour = "00"
    EndIf
    
    $sRetFormat = StringRegExpReplace($sRetFormat, "(?i)(D+)(^/|:|)", $sDay, 1)
    $sRetFormat = StringRegExpReplace($sRetFormat, "(?i)(M+)(^/|:|)", $sMonth, 1)
    $sRetFormat = StringRegExpReplace($sRetFormat, "(?i)(Y+)(^/|:|)", $sYear, 1)
    
    $sRetFormat = StringRegExpReplace($sRetFormat, "(?i)(H+)[^/|:|]", $sHour, 1)
    $sRetFormat = StringRegExpReplace($sRetFormat, "(?i)(M+)[^/|:|]", $sMinute, 1)
    $sRetFormat = StringRegExpReplace($sRetFormat, "(?i)(S+)[^/|:|]", $sSecond, 1)
    
    Return $sRetFormat & $sAM_PM
EndFunc

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

Nice! I once again bow to your greatness lol :P

Hail MrCreatoR

-Kenny

Sometimes i need to convert to different formats, so this function helps me a lot (i just set required format in last parameter):

$sDateTime = "10:19:2008 03:13:55 AM"
$sConverted_DateTime = _DateTimeConvert($sDateTime, 0, "MM:DD:YYYY HH:MM:SS", "YYYY/MM/DD HH:MM:SS")

MsgBox(64, "_DateTimeConvert", $sDateTime & @CRLF & $sConverted_DateTime)

Func _DateTimeConvert($sDateTime, $iConvertAmPm=0, $sDateTimeFormat="DD/MM/YYYY HH:MM:SS", $sRetFormat="YYYY/MM/DD HH:MM:SS")
    Local $sAM_PM = StringStripWS(StringRegExpReplace($sDateTime, "\d+|:|/", ""), 4)
    
    Local $sDay = StringMid($sDateTime, StringInStr($sDateTimeFormat, "DD", 0, 1), 2)
    Local $sMonth = StringMid($sDateTime, StringInStr($sDateTimeFormat, "MM", 0, 1), 2)
    Local $sYear = StringMid($sDateTime, StringInStr($sDateTimeFormat, "YYYY", 0, 1), 4)
    
    Local $sHour = StringMid($sDateTime, StringInStr($sDateTimeFormat, "HH", 0, 1), 2)
    Local $sMinute = StringMid($sDateTime, StringInStr($sDateTimeFormat, "MM", 0, 2), 2)
    Local $sSecond = StringMid($sDateTime, StringInStr($sDateTimeFormat, "SS", 0, 1), 2)
    
    If $iConvertAmPm And StringStripWS($sAM_PM, 8) = "PM" Then
        If $sHour < 12 Then $sHour += 12
        If $sHour = 12 Then $sHour = "00"
    EndIf
    
    $sRetFormat = StringRegExpReplace($sRetFormat, "(?i)(D+)(^/|:|)", $sDay, 1)
    $sRetFormat = StringRegExpReplace($sRetFormat, "(?i)(M+)(^/|:|)", $sMonth, 1)
    $sRetFormat = StringRegExpReplace($sRetFormat, "(?i)(Y+)(^/|:|)", $sYear, 1)
    
    $sRetFormat = StringRegExpReplace($sRetFormat, "(?i)(H+)[^/|:|]", $sHour, 1)
    $sRetFormat = StringRegExpReplace($sRetFormat, "(?i)(M+)[^/|:|]", $sMinute, 1)
    $sRetFormat = StringRegExpReplace($sRetFormat, "(?i)(S+)[^/|:|]", $sSecond, 1)
    
    Return $sRetFormat & $sAM_PM
EndFunc

My Contributions _StringMultiReplace PC Builders Console - Secure PDF Creator - Cisco VPN Installer MS DNS Server Backup Script - MS DHCP Backup Script IT Admin Console - Toggle Admin Mode - MyMovies-Add Discs Script - IT Help Desk and System Information Tool - Set On Lid Close Power Option - Streaming Media Server & Website "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

I changed this one part to drop the AM/PM when the the convert from 12 to 24 is specified

If $iConvertAmPm And StringStripWS($sAM_PM, 8) = "PM" Then
        If $sHour < 12 Then
            $sHour += 12
            $sAM_PM = ""
        EndIf
        If $sHour = 12 Then $sHour = "00"
    EndIf
        If $iConvertAmPm And StringStripWS($sAM_PM, 8) = "AM" Then $sAM_PM = ""

Doesn't seem to handle single digits for the Hour/MM/DD though. I'm experimenting lol

Edited by ken82m

My Contributions _StringMultiReplace PC Builders Console - Secure PDF Creator - Cisco VPN Installer MS DNS Server Backup Script - MS DHCP Backup Script IT Admin Console - Toggle Admin Mode - MyMovies-Add Discs Script - IT Help Desk and System Information Tool - Set On Lid Close Power Option - Streaming Media Server & Website "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Share this post


Link to post
Share on other sites

and why not using my very short solution?

#include <misc.au3>; for _Iif(). This is a builtin function in well-engineered languages :-(
_Convert("10:05:1999 06:23:55 AM")
_Convert("10:19:2008 06:23:55 PM")
_Convert("10/23/2007 07:43:35 PM")

Func _Convert($s1)
    $a = StringSplit($s1, "/: ")
    $s2 = $a[3] & "/" & $a[1] & "/" & $a[2] & " " & _Iif($a[7] = "PM", $a[4] + 12, $a[4]) & ":" & $a[5] & ":" & $a[6]
    MsgBox(0, $s1, $s2)
EndFunc   ;==>_Convert
What happens when it's 12 pm? It goes to 24 not 00 :P

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 this post


Link to post
Share on other sites

hmm that works lol

Here's a modified version of MrCreatoR's, works quite well.

Has some flexibility with input and output

Thanks for the help guys.

-Kenny

#include <array.au3>

;Example 1
$sDateTime = "1/13/2008 2:25:55AM"
$sConverted_DateTime = _DateTimeConvert($sDateTime, 1, "MM/DD/YYYY HH:MM:SS", "YYYY/MM/DD HH:MM:SS")
MsgBox(64, "_DateTimeConvert", $sDateTime & @CRLF & $sConverted_DateTime)

;Example 2
$sDateTime = "10:19:2008 03:13:55 PM"
$sConverted_DateTime = _DateTimeConvert($sDateTime, 0, "MM:DD:YYYY HH:MM:SS", "YYYY/MM/DD HH:MM:SS")
MsgBox(64, "_DateTimeConvert", $sDateTime & @CRLF & $sConverted_DateTime)

;Example 3
$sDateTime = "5:19:07 03:13 PM"
$sConverted_DateTime = _DateTimeConvert($sDateTime, 1, "MM:DD:YYYY HH:MM", "YYYY/MM/DD HH:MM")
MsgBox(64, "_DateTimeConvert", $sDateTime & @CRLF & $sConverted_DateTime)

Func _DateTimeConvert($sDateTime, $iConvertAmPm=0, $sDateTimeFormat="MM/DD/YYYY HH:MM:SS", $sRetFormat="YYYY/MM/DD HH:MM:SS")
    $sAM_PM = StringStripWS($sDateTime, 4)
    If StringRight($sAM_PM, 2) = "AM" OR StringRight($sAM_PM, 2) = "PM" AND StringMid($sAM_PM, StringLen($sAM_PM) - 2, 1) <> " " Then
        $sAM_PM = StringReplace($sAM_PM, "AM", " AM")
        $sAM_PM = StringReplace($sAM_PM, "PM", " PM")
    EndIf
    $sAM_PM = StringSplit($sAM_PM, "\d+|:|/ ")
    Local $sFormatArray = StringSplit($sDateTimeFormat, "\d+|:|/ ")
    
    $sMonPos = _ArraySearch($sFormatArray, "MM")
    $sDayPos = _ArraySearch($sFormatArray, "DD")
    $sYearPos = _ArraySearch($sFormatArray, "YYYY")
    $sHourPos = _ArraySearch($sFormatArray, "HH")
    $sMinPos = _ArraySearch($sFormatArray, "MM", -1, -1, -1, -1, 0)
    $sSecPos = _ArraySearch($sFormatArray, "SS")
    
    If StringLen($sAM_PM[$sMonPos]) = 1 Then $sAM_PM[$sMonPos] = "0" & $sAM_PM[$sMonPos]
    If StringLen($sAM_PM[$sDayPos]) = 1 Then $sAM_PM[$sDayPos] = "0" & $sAM_PM[$sDayPos]
    If StringLen($sAM_PM[$sYearPos]) = 2 Then $sAM_PM[$sYearPos] = "20" & $sAM_PM[$sYearPos]
    If StringLen($sAM_PM[$sHourPos]) = 1 Then $sAM_PM[$sHourPos] = "0" & $sAM_PM[$sHourPos]
    
    $sMonth  = $sAM_PM[$sMonPos]
    $sDay   = $sAM_PM[$sDayPos]
    $sYear   = $sAM_PM[$sYearPos]
    $sHour   = $sAM_PM[$sHourPos]
    $sMinute = $sAM_PM[$sMinPos]
    If $sSecPos <> -1 Then $sSecond = $sAM_PM[$sSecPos]
    
    If $iConvertAmPm = 1 AND $sAM_PM[$sAM_PM[0]] = "PM" Then
        If $sHour < 12 Then
            $sHour += 12
            If StringLen($sAM_PM[$sHourPos]) = 1 Then $sAM_PM[$sHourPos] = "0" & $sAM_PM[$sHourPos]
            $sRetAMPM = ""
        EndIf
    ElseIf $iConvertAmPm = 1 AND $sAM_PM[$sAM_PM[0]] = "AM" Then
        If $sHour = 12 Then $sHour = "00"
        If StringLen($sAM_PM[$sHourPos]) = 1 Then $sAM_PM[$sHourPos] = "0" & $sAM_PM[$sHourPos]
        $sRetAMPM = ""
    ElseIf $iConvertAmPm = 0 AND $sAM_PM[$sAM_PM[0]] = "AM" OR $sAM_PM[$sAM_PM[0]] = "PM" Then
        $sRetAMPM = $sAM_PM[$sAM_PM[0]]
    ElseIf $iConvertAmPm = 1 AND $sAM_PM[$sAM_PM[0]] <> "AM" AND $sAM_PM[$sAM_PM[0]] <> "PM" Then
        $sRetAMPM = ""
    EndIf
    
    If $sDayPos <> -1 Then $sRetFormat = StringRegExpReplace($sRetFormat, "(?i)(D+)(^/|:|)", $sDay, 1)
    If $sMonPos <> -1 Then $sRetFormat = StringRegExpReplace($sRetFormat, "(?i)(M+)(^/|:|)", $sMonth, 1)
    If $sYearPos <> -1 Then $sRetFormat = StringRegExpReplace($sRetFormat, "(?i)(Y+)(^/|:|)", $sYear, 1)
    If $sHourPos <> -1 Then $sRetFormat = StringRegExpReplace($sRetFormat, "(?i)(H+)[^/|:|]", $sHour, 1)
    If $sMinPos <> -1 Then $sRetFormat = StringRegExpReplace($sRetFormat, "(?i)(M+)[^/|:|]", $sMinute, 1)
    If $sSecPos <> -1 Then $sRetFormat = StringRegExpReplace($sRetFormat, "(?i)(S+)[^/|:|]", $sSecond, 1)
    Return $sRetFormat & " " & $sRetAMPM
EndFunc

My Contributions _StringMultiReplace PC Builders Console - Secure PDF Creator - Cisco VPN Installer MS DNS Server Backup Script - MS DHCP Backup Script IT Admin Console - Toggle Admin Mode - MyMovies-Add Discs Script - IT Help Desk and System Information Tool - Set On Lid Close Power Option - Streaming Media Server & Website "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Share this post


Link to post
Share on other sites

Nice :P. It's can be done without Array.au3 funcs:

;Example 1
$sDateTime = "1/13/2008 2:25:55PM"
$sConverted_DateTime = _DateTimeConvert($sDateTime, 0, "M/DD/YYYY H:MM:SS", "YYYY/MM/DD HH:MM:SS PM")
MsgBox(64, "_DateTimeConvert - Example 1", $sDateTime & @CRLF & $sConverted_DateTime)

;Example 2
$sDateTime = "10:19:2008 03:13:55 AM"
$sConverted_DateTime = _DateTimeConvert($sDateTime, 0, "MM:DD:YYYY HH:MM:SS", "YYYY/MM/DD HH:MM:SS AM")
MsgBox(64, "_DateTimeConvert - Example 2", $sDateTime & @CRLF & $sConverted_DateTime)

;Example 3
$sDateTime = "5:19:07 03:13 PM"
$sConverted_DateTime = _DateTimeConvert($sDateTime, 1, "M:DD:YY HH:MM", "YYYY/MM/DD HH:MM")
MsgBox(64, "_DateTimeConvert - Example 3", $sDateTime & @CRLF & $sConverted_DateTime)

Func _DateTimeConvert($sDateTime, $iCorrectAmPm=0, $sDTFrmt="DD/MM/YYYY HH:MM:SS", $sRetFrmt="YYYY/MM/DD HH:MM:SS", $sAddYear=20)
    Local $sAM_PM = StringStripWS(StringRegExpReplace($sDateTime, "\d+|:|/", ""), 8)
    
    Local $aDateTime_Parts = StringSplit($sDTFrmt, ":/ ")
    Local $sDay, $sMonth, $sYear, $sHour, $sMinute, $sSecond, $sPart, $iOccur = 1
    
    For $i = 1 To $aDateTime_Parts[0]
        $sPart = StringMid($sDateTime, StringInStr($sDTFrmt, $aDateTime_Parts[$i], 0, $iOccur), StringLen($aDateTime_Parts[$i]))
        
        If StringInStr($aDateTime_Parts[$i], "D") Then
            $sDay = $sPart
        ElseIf $sMonth = "" And StringInStr($aDateTime_Parts[$i], "M") Then
            $sMonth = $sPart
            $iOccur = -1
        ElseIf StringInStr($aDateTime_Parts[$i], "Y") Then
            $sYear = $sPart
        ElseIf StringInStr($aDateTime_Parts[$i], "H") Then
            $sHour = $sPart
        ElseIf $sMonth <> "" And StringInStr($aDateTime_Parts[$i], "M") Then
            $sMinute = $sPart
        ElseIf StringInStr($aDateTime_Parts[$i], "S") Then
            $sSecond = $sPart
        EndIf
    Next
    
    If $iCorrectAmPm And $sAM_PM = "PM" Then
        If $sHour < 12 Then $sHour += 12
        If $sHour = 12 Then $sHour = "00"
    ElseIf $iCorrectAmPm And $sAM_PM = "AM" Then
        If $sHour > 12 Then $sHour -= 12
        If $sHour = "00" Then $sHour = 12
    EndIf
    
    $aDateTime_Parts = StringSplit($sRetFrmt, ":/ ")
    
    For $i = 1 To $aDateTime_Parts[0]
        If $aDateTime_Parts[$i] = "AM" Or $aDateTime_Parts[$i] = "PM" Then
            ContinueLoop
        ElseIf StringInStr($aDateTime_Parts[$i], "D") Then
            $sPart = $sDay
        ElseIf Not StringInStr($sRetFrmt, $sMonth) And StringInStr($aDateTime_Parts[$i], "M") Then
            $sPart = $sMonth
        ElseIf StringInStr($aDateTime_Parts[$i], "Y") Then
            $sYear = StringRight($sYear, StringLen($aDateTime_Parts[$i]))
            $sPart = $sYear
            If StringLen($sYear) < 4 And StringLen($aDateTime_Parts[$i]) = 4 Then $sPart = $sAddYear & $sYear
        ElseIf StringInStr($aDateTime_Parts[$i], "H") Then
            $sPart = $sHour
        ElseIf StringInStr($sRetFrmt, $sMonth) And StringInStr($aDateTime_Parts[$i], "M") Then
            $sPart = $sMinute
        ElseIf StringInStr($aDateTime_Parts[$i], "S") Then
            $sPart = $sSecond
        EndIf
        
        If StringLen($sPart) = 1 Then
            $sPart = "0" & $sPart
            $sPart = StringRight($sPart, StringLen($aDateTime_Parts[$i]))
        EndIf
        
        $sRetFrmt = StringReplace($sRetFrmt, $aDateTime_Parts[$i], $sPart, 1)
    Next
    
    Return $sRetFrmt
EndFunc

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

AM = avant meridian ( 00:00:00 to 12:00:00 )

PM = post meridian ( 12:00:00 to 00:00:00 )

so:

23:59:59 = 11:59:59 PM

Edited by oMBra

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

00:00:00 = 12:00:00 AM

00:00:01 = 12:00:01 AM

11:59:59 = 11:59:59 AM

12:00:00 = 12:00:00

12:00:01 = 12:00:01 PM

basically subtract 12 if the number is > 12

Edited by oMBra

Share this post


Link to post
Share on other sites

srry I did some error coz I was in hurry

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

Nice. The longer version can handle various input and output formats.

But yours is nice and quick for most conversions :P

I modfied yours very slightly below so it can handle different date format inputs.

I think I'll try and combine these. That would make some of my our parms optional.

So, what problem can we solve next lol

-Kenny

#include <misc.au3>; for _IIf(). This is a builtin function in well-engineered languages :-(
_Convert("10:05:1999 06:23:55 AM");06:23
_Convert("1999:10:05 06:23:55 AM");06:23
_Convert("10:19:2008 06:23:55 PM");18:23
_Convert("10/23/2007 11:43:35 AM");11:43
_Convert("10/23/2007 11:43:35 PM");23:43
_Convert("10/23/2007 12:43:35 AM");00:43
_Convert("10/23/2007 12:43:35 PM");12:43
_Convert("10/23/2007 01:43:35 AM");01:43
_Convert("10/23/2007 01:43:35 PM");13:43

Func _Convert($s1)
    MsgBox(0, $s1, _Convert_to_24($s1))
EndFunc  ;==>_Convert

Func _Convert_to_24($s1)
    $a = StringSplit($s1, "/: ")
    If StringLen($a[1]) = 4 Then 
  Return $a[1] & "/" & $a[2] & "/" & $a[3] & " " & _Iif($a[7] = "PM", _Iif($a[4] = 12, "12", $a[4] + 12), _Iif($a[4] = 12, "00", $a[4])) & ":" & $a[5] & ":" & $a[6]
 Else
  Return $a[3] & "/" & $a[1] & "/" & $a[2] & " " & _Iif($a[7] = "PM", _Iif($a[4] = 12, "12", $a[4] + 12), _Iif($a[4] = 12, "00", $a[4])) & ":" & $a[5] & ":" & $a[6]
 EndIf
EndFunc  ;==>_Convert_to_24
Edited by ken82m

My Contributions _StringMultiReplace PC Builders Console - Secure PDF Creator - Cisco VPN Installer MS DNS Server Backup Script - MS DHCP Backup Script IT Admin Console - Toggle Admin Mode - MyMovies-Add Discs Script - IT Help Desk and System Information Tool - Set On Lid Close Power Option - Streaming Media Server & Website "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Share this post


Link to post
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
Sign in to follow this  
Followers 0