qwert

Need refresher on date conversion

9 posts in this topic

#1 ·  Posted

Every now and then, I come upon the need to compare file dates. Then I realize that I don't have any snippet for the necessary conversions, so I search the help and forum for a simple call. Here's what I mean:

FileGetTime returns YYYYMMDDHHMMSS ... which is great for comparing file dates.

But when a user enters a date on a form and I read it (GUICtrlRead), there isn't a format option that matches the above. Even _NowCalc() doesn't have a matching choice.

Is there a Au3 feature or call to convert an entered date to a "file date"?  Or is there a different common denominator format that I should be using for date comparisons?

Thanks in advance for help.

Date Convert.PNG

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

The date shown above can be interpreted in two different ways. Knowing the user's location would help, a bit. Although I haven't used it myself, you could try Melba's Date_Time_Convert UDF. https://www.autoitscript.com/forum/topic/154684-date_time_convert-bugfix-version-27-may-15/

I did some work on interpreting the format returned from the calendar control. You might be able to modify my code (it's rather complicated though, and I don't have much time to explain it). There's a lot of GUI code that you don't need:

 

Edited by czardas

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks for your quick reply.

Melba23's UDF is tantalizingly close. But I don't see anything that handles YYYYMMDDHHMMSS.

This is why I get the uncomfortable feeling that I'm off the main path and am comparing dates the wrong way.

 

BTW, the date I'm reading is formatted M/D/YYYY.

Edited by qwert

Share this post


Link to post
Share on other sites

#4 ·  Posted

Are all your users in the same country? If not then the order may change. You should be able to force the calendar control to always return the same format.

Share this post


Link to post
Share on other sites

#5 ·  Posted

 

Quote

Are all your users in the same country?

Yes ... although I'm not wild about the way the date is formatted in the form's entry field. Having blanks only serves to confuse. But I haven't had time to see if there are alternatives.

 

Share this post


Link to post
Share on other sites

#6 ·  Posted

Maybe this could help some.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#Tidy_Parameters=/tc 3 /reel
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <DateTimeConstants.au3>
#include <GuiDateTimePicker.au3>
#include <Date.au3>

#Region ### START Koda GUI section ### Form=
$hGui = GUICreate("Date Picker", 285, 70, -1, -1)
GUISetBkColor(0xC0C0C0)
$hdate1 = GUICtrlCreateDate("", 16, 26, 250, 20)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

GUICtrlSetData($hdate1, _Now())
$hWndDate1 = ControlGetHandle($hGui, "", $hdate1)
_GUICtrlDTP_SetFormat($hWndDate1, "MM/dd/yyyy")

While 1
   $msg = GUIGetMsg()
   Switch $msg
      Case -3
         Quit()
      Case $hdate1
         Create_File()
   EndSwitch
WEnd

Func Create_File()
   FileDelete("all_reports.acsauto")

   $date_array = StringSplit(GUICtrlRead($hdate1), "/")

   $yDate = $date_array[3]
   $mDate = $date_array[1]
   $dDate = $date_array[2]

   MsgBox(0, "Done", $yDate&$mDate&$dDate)
EndFunc   ;==>Create_File

Func Quit()
   Exit
EndFunc   ;==>Quit

 


 

Spoiler

WinSizer 2.1 (01/04/2017) - Download - [ Windows Layout Manager ]
Folder+Program (12/23/2016) - Download - [ USB Shortcut Creator ]

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

In my code you can find an example of forcing a four digit year in the control:

; force a four digit year in the date control
    GUICtrlSendMsg($idDate1, 0x1032, 0, LongYearFormat()) ; what is this magic?
    GUICtrlSendMsg($idDate2, 0x1032, 0, LongYearFormat())
    ; https://www.autoitscript.com/forum/topic/93701-how-to-format-date-time-picker-selected-date/#comment-673255 [Melba23]

and

Func LongYearFormat() ; convert yy to yyyy
    Local $iID = _WinAPI_GetUserDefaultLCID(), _
    $sFormat = _WinAPI_GetLocaleInfo($iID, $LOCALE_SSHORTDATE)
    $sFormat = StringRegExpReplace($sFormat, '(?i)(\byy\b)', 'yyyy')
    Return $sFormat
EndFunc ;==> LongYearFormat

With modification, you can force the calendar to always return mm/dd/yyyy (or similar). Then you can swap the order very easily. There are various ways you can do this.

Edit: Or use DTP like @zone97 suggested.

Edited by czardas
1 person likes this

Share this post


Link to post
Share on other sites

#8 ·  Posted

That's more like it! Date Picker solves the problem of blanks on the form ... and gives the format I need for comparisons.

Thanks to you both.

 

Date Picker.PNG

2 people like this

Share this post


Link to post
Share on other sites

#9 ·  Posted

qwert,

Quote

Melba23's UDF is tantalizingly close. But I don't see anything that handles YYYYMMDDHHMMSS

The UDF handles it very well - as you can see here:

#include "DTC.au3"

; Convert from YYYYMMDDHHMMSS to M/D/YYYY
$sIn_Date = "20170107123456"

$sOut_Date = _Date_Time_Convert($sIn_Date, "yyyyMMddHHmmss", "M/d/yyyy")

ConsoleWrite($sOut_Date & @CRLF)

; And back again

$sIn_Date = $sOut_Date

$sOut_Date = _Date_Time_Convert($sIn_Date, "M/d/yyyy", "yyyyMMdd000000") ; No time defined so force to "000000"

ConsoleWrite($sOut_Date & @CRLF)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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