Jump to content

Convert to date/time


Recommended Posts

Hi all,

I want to build a function to convert string format of date/time to real value, but the problem is, that there two strings of MM (mounth and minutes)...

Here is what i mean:

$FullDateTime = _DateTimeConvertFormat("DD/MM/YYYY, HH:MM:SS")
MsgBox(64, "Date/Time Convert Results", $FullDateTime)

Func _DateTimeConvertFormat($sFormat)
    Local $RetDateTime
    Local $Year_Raw = @YEAR
    Local $Year_Part = StringRight($Year_Raw, 2)
    
    Local $Mounth_Raw = @MON
    Local $Mounth_Part = StringRight($Mounth_Raw, 1)
    If $Mounth_Raw > 9 Then $Mounth_Part = $Mounth_Raw
    
    Local $Day_Raw = @MDAY
    Local $Day_Part = StringRight($Day_Raw, 1)
    If $Day_Raw > 9 Then $Day_Part = $Day_Raw
    
    Local $Hours_Raw = @HOUR
    Local $Hours_Part = StringRight($Hours_Raw, 1)
    If $Hours_Raw > 9 Then $Hours_Part = $Hours_Raw
    
    Local $Minutes_Raw = @MIN
    Local $Minutes_Part = StringRight($Minutes_Raw, 1)
    If $Minutes_Raw > 9 Then $Minutes_Part = $Minutes_Raw
    
    Local $Seconds_Raw = @SEC
    Local $Seconds_Part = StringRight($Seconds_Raw, 1)
    If $Seconds_Raw > 9 Then $Seconds_Part = $Seconds_Raw
    
    
    $RetDateTime = StringReplace($sFormat, "yyyy", $Year_Raw)
    $RetDateTime = StringRegExpReplace($RetDateTime, "(?i)y+", $Year_Part)
    
    $RetDateTime = StringReplace($RetDateTime, "mm", $Mounth_Raw, 1)
    
    $RetDateTime = StringReplace($RetDateTime, "dd", $Day_Raw)
    $RetDateTime = StringReplace($RetDateTime, "d", $Day_Part)
    
    
    $RetDateTime = StringReplace($RetDateTime, "hh", $Hours_Raw)
    $RetDateTime = StringReplace($RetDateTime, "h", $Hours_Part)
    
    $RetDateTime = StringReplace($RetDateTime, "mm", $Minutes_Raw)
    $RetDateTime = StringReplace($RetDateTime, "m", $Minutes_Part)
    
    $RetDateTime = StringReplace($RetDateTime, "ss", $Seconds_Raw)
    $RetDateTime = StringReplace($RetDateTime, "s", $Seconds_Part)
    
    Return $RetDateTime
EndFunc

If we set the string like this:

$FullDateTime = _DateTimeConvertFormat("DD/M/YYYY, HH:MM:SS")

Then we get switched mounth with minutes...

And also, is there maybe an easyer (faster/quicker) way to make such function?

 

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

Link to comment
Share on other sites

Thanks, but it's not an option, the string can be like this:

"DD/MM/YYYY_HH:MM:SS"

or like this:

"HH:MM:SS DD/MM/YYYY"

Must converted as it is (without replacing any character except the date/time format.

Here is what i came up with at the end(?):

$Str = 'DD/MM/YYYY, [HH;MM;SS]'

ConsoleWrite(_DateTimeConvertFormat($Str))

Func _DateTimeConvertFormat($sFormat)
    Local $RetDateTime = $sFormat
    
    If StringRegExp($sFormat, '(?i)\[(.*)\]') Then
        $TPos_1 = StringInStr($sFormat, "[")
        $TPos_2 = StringInStr($sFormat, "]")
        
        $TruimVal = 1
        If StringInStr(StringMid($sFormat, $TPos_2 + 1), "]") Then $TruimVal = 0
        
        $TimeStr = StringMid($sFormat, $TPos_1+1, ($TPos_2 - $TPos_1) - $TruimVal)
        $TimeStr = _TimeConvertFormat($TimeStr)
        $DateStr = _DateConvertFormat(StringRegExpReplace($sFormat, '(?i)\[(.*)\]', '', 1))
        If StringRegExp(StringMid($sFormat, $TPos_2), '(?i)D|MM/Y') Then
            $RetDateTime = $TimeStr & $DateStr
        Else
            $RetDateTime = $DateStr & $TimeStr
        EndIf
    Else
        $RetDateTime = _DateConvertFormat($sFormat)
    EndIf
    
    If $RetDateTime = $sFormat Then $RetDateTime = _DateConvertFormat($sFormat)
    Return $RetDateTime
EndFunc

Func _TimeConvertFormat($sFormat)
    Local $RetDateTime = $sFormat
    
    Local $Hours_Raw = @HOUR
    Local $Hours_Part = StringRight($Hours_Raw, 1)
    If $Hours_Raw > 9 Then $Hours_Part = $Hours_Raw
    
    Local $Minutes_Raw = @MIN
    Local $Minutes_Part = StringRight($Minutes_Raw, 1)
    If $Minutes_Raw > 9 Then $Minutes_Part = $Minutes_Raw
    
    Local $Seconds_Raw = @SEC
    Local $Seconds_Part = StringRight($Seconds_Raw, 1)
    If $Seconds_Raw > 9 Then $Seconds_Part = $Seconds_Raw
    
    $RetDateTime = StringReplace($RetDateTime, "hh", $Hours_Raw)
    $RetDateTime = StringReplace($RetDateTime, "h", $Hours_Part)
    
    $RetDateTime = StringReplace($RetDateTime, "mm", $Minutes_Raw)
    $RetDateTime = StringReplace($RetDateTime, "m", $Minutes_Part)
    
    $RetDateTime = StringReplace($RetDateTime, "ss", $Seconds_Raw)
    $RetDateTime = StringReplace($RetDateTime, "s", $Seconds_Part)
    
    Return $RetDateTime
EndFunc

Func _DateConvertFormat($sFormat)
    Local $RetDateTime = $sFormat
    
    StringReplace($RetDateTime, "y", "")
    Local $Year = StringRight(@YEAR, @extended)
    
    Local $Mounth_Raw = @MON
    Local $Mounth_Part = StringRight($Mounth_Raw, 1)
    If $Mounth_Raw > 9 Then $Mounth_Part = $Mounth_Raw
    
    Local $Day_Raw = @MDAY
    Local $Day_Part = StringRight($Day_Raw, 1)
    If $Day_Raw > 9 Then $Day_Part = $Day_Raw
    
    $RetDateTime = StringReplace($RetDateTime, "y", $Year, 1)
    $RetDateTime = StringReplace($RetDateTime, "y", "")
    
    $RetDateTime = StringReplace($RetDateTime, "mm", $Mounth_Raw)
    $RetDateTime = StringReplace($RetDateTime, "m", $Mounth_Part)
    
    $RetDateTime = StringReplace($RetDateTime, "dd", $Day_Raw)
    $RetDateTime = StringReplace($RetDateTime, "d", $Day_Part)
    Return $RetDateTime
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

Link to comment
Share on other sites

Ok, here is the final version, works greate, thanks to amel27:

$FullDateTime = _DateTimeConvertFormat('MM/DD/YYYY, [HH:MM:SS]')
MsgBox(64, "Date/Time Convert Results", $FullDateTime)

Func _DateTimeConvertFormat($sFormat)
    Local $Stamp = '<_>'
    While StringInStr($sFormat, $Stamp)
        $Stamp = '<' & Random(10000000,99999999,1) & '>'
    WEnd
    
    StringReplace($sFormat, "y", "")
    Local $Year = StringRight(@YEAR, @extended)
    Local $Mounth = @MON
    Local $Day = @MDAY
    Local $Hours = @HOUR
    Local $Minutes = @MIN
    Local $Seconds = @SEC

    $sFormat = StringRegExpReplace($sFormat, "(?i)(?:(d+|y+)(\W*)(mm)|(mm)(\W*)(d+|y+))", '\1\2' & $Stamp & '\5\6')
    If @extended Then $sFormat = StringReplace($sFormat, $Stamp, $Mounth)
    $sFormat = StringRegExpReplace($sFormat, "(?i)(?:(d+|y+)(\W*)(m)|(m)(\W*)(d+|y+))", '\1\2' & $Stamp & '\5\6')
    If @extended Then $sFormat = StringReplace($sFormat, $Stamp, $Mounth+0)
    
    $sFormat = StringRegExpReplace($sFormat, '(?i)y+', $Year)
    
    $sFormat = StringReplace($sFormat, "mm",$Minutes)
    $sFormat = StringReplace($sFormat, "m", $Minutes+0)
    
    $sFormat = StringReplace($sFormat, "dd",$Day)
    $sFormat = StringReplace($sFormat, "d", $Day+0)
    
    $sFormat = StringReplace($sFormat, "hh",$Hours)
    $sFormat = StringReplace($sFormat, "h", $Hours+0)
    
    $sFormat = StringReplace($sFormat, "ss",$Seconds)
    $sFormat = StringReplace($sFormat, "s", $Seconds+0)
    
    Return $sFormat
EndFunc

Edit: I don't know why, but in autoit tags the smiles are shown as smiles :) - so i edited it to standard code tags.

Edited by MsCreatoR

 

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

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