litlmike Posted April 8, 2018 Posted April 8, 2018 I am taking a Date & Time and splitting them using _DateTimeSplit, so that I can create a filename in the format: YYYYMMDD_HHMMSS.jpg. The only issue is that if the month, or day, is a two-digit number that begins with a zero (so less than 10), then the month, or day, is formatted as a single-digit number. Suggestions on how to accomplish this? Example: 20180101 becomes 2018, 1, 1 20180303 becomes 2018, 3, 3 20180505 becomes 2018, 5, 5 20180606 becomes 2018, 6, 6 20180909 becomes 2018, 9, 9 What I want: 20180101 becomes 2018, 01, 01 20180303 becomes 2018, 03, 03 20180505 becomes 2018, 05, 05 20180606 becomes 2018, 06, 06 20180909 becomes 2018, 09, 09 Func _CreateFileNameFromDateTimeFormat($sDateThatWasChangedIntoDateTimeFormat = "2018/01/02 10:29:39") Local $aMyDate, $aMyTime, $sNewImageFileName _DateTimeSplit($sDateThatWasChangedIntoDateTimeFormat, $aMyDate, $aMyTime) _ArrayDisplay($aMyDate) _ArrayDisplay($aMyTime) ;This will become file name $sNewImageFileName = $aMyDate[1] & $aMyDate[2] & $aMyDate[3] & "_" & $aMyTime[1] & $aMyTime[2] & $aMyTime[3] & ".jpg" ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sNewImageFileName = ' & $sNewImageFileName & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console EndFunc ;==>_CreateFileNameFromDateTimeFormat _ArrayPermute()_ArrayUnique()Excel.au3 UDF
Developers Jos Posted April 8, 2018 Developers Posted April 8, 2018 Something like this? : ;This will become file name $sNewImageFileName = StringFormat("%04i%02i%02i_%02i%02i%02i.jpg",$aMyDate[1],$aMyDate[2],$aMyDate[3],$aMyTime[1],$aMyTime[2],$aMyTime[3]) Jos litlmike 1 SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
litlmike Posted April 8, 2018 Author Posted April 8, 2018 2 minutes ago, Jos said: Something like this? : ;This will become file name $sNewImageFileName = StringFormat("%04i%02i%02i_%02i%02i%02i.jpg",$aMyDate[1],$aMyDate[2],$aMyDate[3],$aMyTime[1],$aMyTime[2],$aMyTime[3]) Jos Ahhhh! Thanks for that! I was about to do this, which is far less elegant: ;Make sure that the array still contains the zero in the format, so that 7 remains as 07 If $aMyDate[2] < 10 Then $aMyDate[2] = String(0 & $aMyDate[2]) EndIf If $aMyDate[3] < 10 Then $aMyDate[3] = String(0 & $aMyDate[3]) EndIf If $aMyTime[1] < 10 Then $aMyTime[1] = String(0 & $aMyTime[1]) EndIf If $aMyTime[2] < 10 Then $aMyTime[2] = String(0 & $aMyTime[2]) EndIf If $aMyTime[3] < 10 Then $aMyTime[3] = String(0 & $aMyTime[3]) EndIf _ArrayPermute()_ArrayUnique()Excel.au3 UDF
Danyfirex Posted April 8, 2018 Posted April 8, 2018 RegExp alternative. Local $sNewImageFileName = StringRegExpReplace($sDateThatWasChangedIntoDateTimeFormat, '(\d{4})/(\d{2})/(\d{2}) (\d{2}):(\d{2}):(\d{2})', '$1$2$3_$4$5$6') & ".jpg" Saludos Danysys.com AutoIt... UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut
iamtheky Posted April 8, 2018 Posted April 8, 2018 (edited) or forget what it represents and just split the string, since its already exactly like you want it _CreateFileNameFromDateTimeFormat() Func _CreateFileNameFromDateTimeFormat($sDateThatWasChangedIntoDateTimeFormat = "2018/01/02 10:29:39") $aDateTime = stringsplit($sDateThatWasChangedIntoDateTimeFormat , "/: " , 2) $sNewImageFileName = $aDateTime[0] & $aDateTime[1] & $aDateTime[2] & "_" & $aDateTime[3] & $aDateTime[4] & $aDateTime[5] & ".jpg" ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sNewImageFileName = ' & $sNewImageFileName & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console EndFunc ;==>_CreateFileNameFromDateTimeFormat all in all, pretty much this msgbox(0, '' , _CreateFileNameFromDateTimeFormat()) Func _CreateFileNameFromDateTimeFormat($sDateThatWasChangedIntoDateTimeFormat = "2018/01/02 10:29:39") return stringreplace(stringreplace(StringReplace($sDateThatWasChangedIntoDateTimeFormat , "/" , "") , ":" , "") , " " , "_") & ".jpg" EndFunc ;==>_CreateFileNameFromDateTimeFormat Edited April 8, 2018 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
antonioj84 Posted April 8, 2018 Posted April 8, 2018 $Start=123 MsgBox(4096,"",StringFormat("%04i", $Start )) not sure if this can help you can format like that
litlmike Posted April 8, 2018 Author Posted April 8, 2018 Thanks everyone, going with Jos' solution! _ArrayPermute()_ArrayUnique()Excel.au3 UDF
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