Jump to content

help converting dates to consistent format


Recommended Posts

i am working with a list of dates - depending on the date returned sometimes the result is formatted in the formats shown below

M/dd/yyyy

M/d/yyyy

MM/dd/yyyy

MM/d/yyyy

(at least year is consistently yyyy)  

Is there any way to convert any of these types to a consistent format (ie MM/dd/yyyy)

i see scripts on here that convert from a syntax to another.. but the input syntax has to specify what format the input date is in

i was thinking doing a stringsplit of the "/" character and then inserting zeroes where appropriate then piecing the values back together but that doesnt seem as efficient

#include <array.au3>

$msg_normal = 0

$date = "2/3/2012"
;~ $date = "2/03/2012"
;~ $date = "02/3/2012"
;~ $date = "02/03/2012"

$date_formatted = FormatDate($date)

Debug($date_formatted)

Func FormatDate($old_date)

    $array = StringSplit($old_date, "/")

    For $x = 1 To 2
        If StringLen($array[$x]) = 1 Then
            $array[$x] = "0" & $array[$x]
        EndIf
    Next

    Local $new_date

    For $x = 1 To 3
        $new_date &= $array[$x] & "/"
    Next

    $new_date = StringTrimRight($new_date, 1)

    Return $new_date

EndFunc   ;==>FormatDate

Func Debug($variable1 = "", $variable2 = "", $variable3 = "")

;~  #include <array.au3>
;~  $msg_normal = 0

    If IsArray($variable1) Then
        _ArrayDisplay($variable1)
    Else
        If $variable2 <> "" Then
            $variable1 &= @CRLF & $variable2
        EndIf

        If $variable3 <> "" Then
            $variable1 &= @CRLF & $variable3
        EndIf

        ClipPut($variable1)
        MsgBox($msg_normal, "Debug", $variable1)
    EndIf

EndFunc   ;==>Debug

any thoughts?

thanks in advance!

 

Edited by gcue
Link to comment
Share on other sites

Give this a try.  It should do what you need.  

Global $sDate = ""
$sDate = "2/3/2012"
;~ $sDate = "2/03/2012"
;~ $sDate = "02/3/2012"
;~ $sDate = "02/03/2012"

$sDate = StringRegExpReplace($sDate, "^(\d)/(\d)/(\d{4})$", "0$1/0$2/$3")
$sDate = StringRegExpReplace($sDate, "^(\d{2})/(\d)/(\d{4})$", "$1/0$2/$3")
$sDate = StringRegExpReplace($sDate, "^(\d)/(\d{2})/(\d{4})$", "0$1/$2/$3")

ConsoleWrite($sDate & @CRLF)

 

Adam

Edited by AdamUL
Link to comment
Share on other sites

Give this a try.  It should do what you need.  

Global $sDate = ""
$sDate = "2/3/2012"
;~ $sDate = "2/03/2012"
;~ $sDate = "02/3/2012"
;~ $sDate = "02/03/2012"

$sDate = StringRegExpReplace($sDate, "^(\d)/(\d)/(\d{4})$", "0$1/0$2/$3")
$sDate = StringRegExpReplace($sDate, "^(\d{2})/(\d)/(\d{4})$", "$1/0$2/$3")
$sDate = StringRegExpReplace($sDate, "^(\d)/(\d{2})/(\d{4})$", "0$1/$2/$3")

ConsoleWrite($sDate & @CRLF)

 

Adam

​works great thanks adam!

Link to comment
Share on other sites

A little bit shorter:

Global $sDate = ""
;~ $sDate = "2/3/2012"
;~ $sDate = "2/03/2012"
$sDate = "02/3/2012"

$aDate = StringRegExp($sDate, "(\d+)\/(\d+)\/(\d+)", 3)
$sDateFormat = StringFormat("%02i/%02i/%04i", $aDate[0], $aDate[1], $aDate[2])

ConsoleWrite($sDateFormat & @CRLF)

 

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

i am able to pull time now from those dates.  so now the format varies between h and hh.  minutes and AM/PM are consistent # of characters.  the data looks like this now.

$date = "2/3/2012 8:38 PM"
;~ $date = "2/03/2012 08:38 PM"
;~ $date = "02/3/2012 8:38 AM"
;~ $date = "02/03/2012 08:38 AM"

sorry there will be no more variations of the data. the export of dates i am getting didnt have times before - wont change again

thanks again in advance!

 

Link to comment
Share on other sites

Global $date
;~ $date = "2/3/2012 8:38 PM"
;~ $date = "2/03/2012 08:38 PM"
;~ $date = "02/3/2012 8:38 AM"
;~ $date = "02/03/2012 08:38 AM"

$aDate = StringRegExp($date, "(?i)(\d+)\/(\d+)\/(\d+)\h+(\d+):(\d+)\h+(\w+)", 3)
$sDateFormat = StringFormat("%02i/%02i/%04i %02i:%02i %s", $aDate[0], $aDate[1], $aDate[2], $aDate[3], $aDate[4], $aDate[5])

ConsoleWrite($sDateFormat & @CRLF)

 

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

 

Global $date
;~ $date = "2/3/2012 8:38 PM"
;~ $date = "2/03/2012 08:38 PM"
;~ $date = "02/3/2012 8:38 AM"
;~ $date = "02/03/2012 08:38 AM"

$aDate = StringRegExp($date, "(?i)(\d+)\/(\d+)\/(\d+)\h+(\d+):(\d+)\h+(\w+)", 3)
$sDateFormat = StringFormat("%02i/%02i/%04i %02i:%02i %s", $aDate[0], $aDate[1], $aDate[2], $aDate[3], $aDate[4], $aDate[5])

ConsoleWrite($sDateFormat & @CRLF)

 

 

​beautiful!!! :sweating:

thanks UEZ!

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