Jump to content

How to keep leading zero


Recommended Posts

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

 

 

Link to comment
Share on other sites

  • Developers

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

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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

×
×
  • Create New...