Jump to content
litlmike

How to keep leading zero

Recommended Posts

litlmike

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

 

 

Share this post


Link to post
Share on other sites
Jos

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

  • Thanks 1

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
litlmike
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

 

Share this post


Link to post
Share on other sites
Danyfirex

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

Share this post


Link to post
Share on other sites
iamtheky
Posted (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 by iamtheky

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

Share this post


Link to post
Share on other sites
antonioj84
$Start=123

MsgBox(4096,"",StringFormat("%04i", $Start ))

not sure if this can help you can format like that

Share this post


Link to post
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

  • Similar Content

    • VADemon
      By VADemon
      I've encountered a problem with a single file where I cannot retrieve it's Date-time. So far my code has worked well for over 30 files, but this one is a mystery I cannot debug myself due to insufficient Au3 knowledge.
      In line 11 "_Date_Time_FileTimeToArray" is called and for this particular file it sets the @error to 10. I don't know what that error code means, but it's not set by the _Date functions themselves I think.
      Overall, it could be a problem caused by any of the functions below, how can I properly debug this? / Does anybody know a what's causing this?
      _WinAPI_CreateFile() / _Date_Time_GetFileTime() / _Date_Time_FileTimeToArray()
      Func _SetFileTimes($sFilePath) Local $monthNumber[13] = ["", "January", "February", "March", "April", "May", "Juny", "July", "August", "September", "October", "November", "December"] Local $dayNumber[7] = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] Local $fHandle = _WinAPI_CreateFile($sFilePath, 2, 2) ; read-only ; may NOT return a valid date for some reason! TODO Local $fTagFILETIME = _Date_Time_GetFileTime($fHandle) _WinAPI_CloseHandle($fHandle) ; This will return an empty array if theres no valid date $fModTime = _Date_Time_FileTimeToArray($fTagFILETIME[2]) ; last Modified if @error <> 10 then Local $year = $fModTime[2] Local $month = $fModTime[0] Local $day = $fModTime[1] Local $hour = $fModTime[3] Local $min = $fModTime[4] Local $sec = $fModTime[5] Local $ms = $fModTime[6] Local $weekday = $fModTime[7] Global $prettyTimestamp = StringFormat("%s, %s %d, %04d %02d:%02d:%02d", $dayNumber[$weekday], $monthNumber[$month], $day, $year, $hour, $min, $sec) Global $uploadDate = StringFormat("%04d-%02d-%02d", $year, $month, $day) $fModTime = _Date_Time_FileTimeToArray(_Date_Time_FileTimeToLocalFileTime($fTagFILETIME[2])) ; last Modified Local $year = $fModTime[2] Local $month = $fModTime[0] Local $day = $fModTime[1] Local $hour = $fModTime[3] Local $min = $fModTime[4] Local $sec = $fModTime[5] Local $ms = $fModTime[6] Local $weekday = $fModTime[7] ; GetUnixTime accounts for Local time, hence feed it local time Global $unixTimestamp = _GetUnixTime($year &"/"& $month &"/"& $day &" "& $hour&":"& $min &":"& $sec) else Global $prettyTimestamp = "N/A" Global $uploadDate = "" Global $unixTimestamp = "N/A" endif endfunc  
      _GetUnixTime returned the year 1601 start date, showing that $fModTime is probably equal 0. (But Why?)
      The file reports these dates in Explorer, it's on local NTFS drive:
      Created: ‎‎Wednesday, ‎31. ‎Januar ‎2018, ‏‎18:55:02
      Modified: ‎Wednesday, ‎10. ‎Januar ‎2018, ‏‎12:39:23
      Accessed: ‎Wednesday, ‎10. ‎Januar ‎2018, ‏‎12:39:23
    • Jemboy
      By Jemboy
      Hi,
      Ones(some times twice) a month I get an e-mail with zip file, which has price updates from a supplier.
      I have to upload the file to an FTP to get it processed.
      When uploading the file, it will get "timestamped" with the time and date at which time the file was uploaded.
      Normally this is fine, because I mostly upload the file the same day.
      Sometimes it may take 1 or 2 days before I can upload the file.
      For historical purpose, I would like to have the file timestamped with the original date.
      I have tried using:  _FTP_Command ( $hFTPSession, "MFCT YYYYMMDDHHMMSS path") 
      however this command does not work or change the timestamp as I expected.
      Does anyone now a way how I can change the timestamp of a FTP-file?
    • Siryx
      By Siryx
      So what i need is a function that I pass a starting date into and it counts up to the current date. Example:
      2009-05-30
      2009-05-31
      2009-06-01
      ...
      2018-06-18
      How would I do that? I found the Date.au3 in the includes but I can't find anything close to a time object like I'm used to working with in Java. I just need some $date = setDate(2009-05-30) and from there I could just add a day every time. I need this to be in the very format I stated earlier and from what I can see everytime related to date and time is automatically changed to my german locale.
      €: I already tried setDate but instead it changed my PCs clock. Not quite what i was looking for  
    • nacerbaaziz
      By nacerbaaziz

      Hello
      I have a question please
      How to show Islamic date in Autoit
      I mean
      for example
      Ramadan month
      And moharam month ... etc
      I hope my question is clear for you
      Thanks in advance
    • Jury
      By Jury
      So I get:
      $aMatch[0] Date(1499295600000)   this is supposed to be 06 Jul 2017
      $aMatch[0] Date(1483574400000)  this is supposed to be 05 Jan 2017
      does anybody know the formula to calculate the date from the large number?  It doesn't seem to be anything to do with DateToDayValue.
      thanks in advance.
×