gcue Posted May 19, 2015 Share Posted May 19, 2015 (edited) i am working with a list of dates - depending on the date returned sometimes the result is formatted in the formats shown belowM/dd/yyyyM/d/yyyyMM/dd/yyyyMM/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 ini 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 efficientexpandcollapse popup#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 ;==>Debugany thoughts?thanks in advance! Edited May 19, 2015 by gcue Link to comment Share on other sites More sharing options...
Danp2 Posted May 19, 2015 Share Posted May 19, 2015 From what source are you pulling the data? If you don't know the format, how are you going to differentiate between 01/02/2015 and 02/01/2015? Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
AdamUL Posted May 19, 2015 Share Posted May 19, 2015 (edited) 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 May 19, 2015 by AdamUL Link to comment Share on other sites More sharing options...
Danp2 Posted May 19, 2015 Share Posted May 19, 2015 Am I imagining things or did the original post not include formats like dd/MM/YYYY? Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
gcue Posted May 20, 2015 Author Share Posted May 20, 2015 its always month then day then year.. but the single/double digits vary depending on what month/day it is Link to comment Share on other sites More sharing options...
gcue Posted May 20, 2015 Author Share Posted May 20, 2015 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) Adamworks great thanks adam! Link to comment Share on other sites More sharing options...
UEZ Posted May 20, 2015 Share Posted May 20, 2015 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 More sharing options...
gcue Posted May 20, 2015 Author Share Posted May 20, 2015 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 againthanks again in advance! Link to comment Share on other sites More sharing options...
UEZ Posted May 20, 2015 Share Posted May 20, 2015 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 More sharing options...
gcue Posted May 20, 2015 Author Share Posted May 20, 2015 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!!! thanks UEZ! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now