Sign in to follow this  
Followers 0
pmax

Yyyymmdd - Guictrlcreatedate And _datetimesplit

9 posts in this topic

Hi,

I'm confused: I can easily create date controls in the GUI, but I don't seem to be able to get the date returned into a format that most of the _date functions require. For instance:

#include <GUIConstants.au3>
#include <Date.au3>

; Deduct 1 days to today
$sNewDate = _DateAdd( 'd',-1, _NowCalcDate())


GUICreate("GUI menu",300,200)
GuiSetIcon(@SystemDir & "\mspaint.exe", 0)

; DATE1
GuiCtrlCreateLabel("From", 10, 10, 200, 22)
$dtFrom = GuiCtrlCreateDate($sNewDate, 10, 30, 200, 20)


$cancelbutton = GuiCtrlCreateButton ("Cancel",50,130,70,20)

$okbutton = GuiCtrlCreateButton ("OK",180,130,70,20)

GuiSetState()

While 1
    $msg = GUIGetMsg()
    

    Select
        Case $msg = $GUI_EVENT_CLOSE Or $msg = $cancelbutton
            ExitLoop
        
        Case $msg = $okbutton
            MsgBox(0, "OK",GUICtrlRead($dtFrom))
            ExitLoop
    EndSelect
WEnd
msgbox(0,"Is date?",_DateIsValid(GUICtrlRead($dtFrom)))

GUIDelete()
Exit

_DateIsValid returns 0 (Failure: Returns 0 if the specified date is not valid. )

How do I reliably convert the GUICtrlRead($dtFrom) back into "YYYYMMDDHHMMSS"?

(for instance to use with FileSetTime)

I have also tried

$dtFrom = GuiCtrlCreateDate($sNewDate, 10, 30, 200, 20, $DTS_SHORTDATEFORMAT)

Should there be some way to force the date control to retun in yyyymmdd format ($DTS_YYYYMMDD perhaps)

Thanks,

Paul

Share this post


Link to post
Share on other sites



Not tested: (Coz I'm currently not running Windows):

#include <GUIConstants.au3>
#include <Date.au3>

; Deduct 1 days to today
$sNewDate = _DateAdd( 'd',-1, _NowCalcDate())


GUICreate("GUI menu",300,200)
GuiSetIcon(@SystemDir & "\mspaint.exe", 0)

$dtFrom = GuiCtrlCreateDate($sNewDate, 10, 30, 200, 20, $DTS_SHORTDATEFORMAT)

$okbutton = GuiCtrlCreateButton ("OK",180,130,70,20)

GuiSetState()

While 1
    $msg = GUIGetMsg()
    

    Select
        Case $msg = $GUI_EVENT_CLOSE Or $msg = $cancelbutton
            ExitLoop
        
        Case $msg = $okbutton
$date_111= _FormatDate($dtFrom)
            MsgBox(0, "OK", $date_111)
            ExitLoop
    EndSelect
WEnd
msgbox(0,"Is date?",_DateIsValid($date_111))

GUIDelete()
Exit

Func _FormatDate($ctrl)
  $data = GUICtrlRead($ctrl)
  $data = StringSplit($data, "\/")
  If Number($data[3]) > 50 Then
    $data[3] = "19" & $data[3]
  Else
    $data[3] = "20" & $data[3]
  EndIf
  return $data[1] & $data[2] & $data[3]
EndFunc

#)

Share this post


Link to post
Share on other sites

yeah u added in a $cancelbutton :)

#include <GUIConstants.au3>
#include <Date.au3>

; Deduct 1 days to today
$sNewDate = _DateAdd( 'd',-1, _NowCalcDate())


GUICreate("GUI menu",300,200)
GuiSetIcon(@SystemDir & "\mspaint.exe", 0)

$dtFrom = GuiCtrlCreateDate($sNewDate, 10, 30, 200, 20, $DTS_SHORTDATEFORMAT)

$okbutton = GuiCtrlCreateButton ("OK",180,130,70,20)

GuiSetState()

While 1
    $msg = GUIGetMsg()
    

    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        
        Case $msg = $okbutton
$date_111= _FormatDate($dtFrom)
            MsgBox(0, "OK", $date_111)
            ExitLoop
    EndSelect
WEnd
msgbox(0,"Is date?",_DateIsValid($date_111))

GUIDelete()
Exit

Func _FormatDate($ctrl)
  $data = GUICtrlRead($ctrl)
  $data = StringSplit($data, "\/")
  If Number($data[3]) > 50 Then
    $data[3] = "19" & $data[3]
  Else
    $data[3] = "20" & $data[3]
  EndIf
  return $data[1] & $data[2] & $data[3]
EndFunc

just debugged it didnt change anything but deleting that

Share this post


Link to post
Share on other sites

What's interesting is running the example from the helpfile tells my computer's date pulled from the time macros is invalid... Does it work for anyone else?

#include <Date.au3>

$sDate = @MON & "/" & @MDAY & "/" & @YEAR

If _DateIsValid( $sDate ) Then
  MsgBox( 4096, "Valid Date", "The specified date is valid." )
Else
  MsgBox( 4096, "Valid Date", "The specified date is invalid." )
EndIf

Share this post


Link to post
Share on other sites

This variant works, note that _DateIsValid requires delimiters,

so the StringRight("0" & data[n],2) probably isn't needed

and that if yyyy is returned then I've included

If Number($data[3]) < 1000 then

#include <GUIConstants.au3>
#include <Date.au3>

; Deduct 1 days to today
$sNewDate = _DateAdd( 'd',-1, _NowCalcDate())


GUICreate("GUI menu",300,200)
GuiSetIcon(@SystemDir & "\mspaint.exe", 0)

; DATE1
GuiCtrlCreateLabel("From", 10, 10, 200, 22)
$dtFrom = GuiCtrlCreateDate($sNewDate, 10, 30, 200, 20, $DTS_SHORTDATEFORMAT)


$cancelbutton = GuiCtrlCreateButton ("Cancel",50,130,70,20)

$okbutton = GuiCtrlCreateButton ("OK",180,130,70,20)

GuiSetState()

While 1
    $msg = GUIGetMsg()
    

    Select
    Case $msg = $GUI_EVENT_CLOSE Or $msg = $cancelbutton
        ExitLoop
        
        Case $msg = $okbutton
            $date_111= _FormatDate($dtFrom)
            MsgBox(0, "OK", $date_111)
            ExitLoop
    EndSelect
WEnd
msgbox(0,"Is date?",_DateIsValid($date_111))

GUIDelete()
Exit

Func _FormatDate($ctrl)
  $data = GUICtrlRead($ctrl)
  $data = StringSplit($data, "\/")
  If Number($data[3]) < 1000 then
     if Number($data[3]) > 50 Then
        $data[3] = "19" & $data[3]
     Else
        $data[3] = "20" & $data[3]
     EndIf
  EndIf
  return $data[3] & "/" & StringRight("0" & $data[2],2) & "/" & StringRight("0" & $data[1],2)
EndFunc

I'd still like to see some more consistency between the various date related items (eg. FileSetTime does not want delimiters but _DateIsValid does need them.

Thanks everyone!

Share this post


Link to post
Share on other sites

This variant works, note that _DateIsValid requires delimiters,

so the StringRight("0" & data[n],2) probably isn't needed

and that if yyyy is returned then I've included

If Number($data[3]) < 1000 then

I'd still like to see some more consistency between the various date related items (eg. FileSetTime does not want delimiters but _DateIsValid does need them.

Thanks everyone!

I tried the example from the helpfile again after you mentioned this.. and it seems that the helpfile is fibbing when it says

This function takes a date input in one of the following formats:

m/d/yy, m/d/yyyy, mm/dd/yy, mm/dd/yyyy

d.m.yy, d.m.yyyy, dd.mm.yy, dd.mm.yyyy

m_d_yy, m_d_yyyy, mm_dd_yy, mm_dd_yyyy

yy-m-d, yyyy-m-d, yy-mm-dd, yyyy-mm-dd

Because it DOES work if I switch the formatting arround to be Year/Day/Month.. but not Month/Day/Year.

Share this post


Link to post
Share on other sites

Got It! (I hope) - thanks for the clues. Neeed to account for m/d/y as well as d/m/y etc..

#include <GUIConstants.au3>
#include <Date.au3>


;get registry entry for short date 
$dStyle = RegRead("HKEY_CURRENT_USER\Control Panel\International", "sShortDate")


; Deduct 1 days to today
$sNewDate = _DateAdd( 'd',-1, _NowCalcDate())


GUICreate("GUI menu",300,200)
GuiSetIcon(@SystemDir & "\mspaint.exe", 0)

; DATE1
GuiCtrlCreateLabel("From Date: (" & $dStyle & ")", 10, 10, 200, 22)
$dtFrom = GuiCtrlCreateDate($sNewDate, 10, 30, 200, 20, $DTS_SHORTDATEFORMAT)


$cancelbutton = GuiCtrlCreateButton ("Cancel",50,130,70,20)

$okbutton = GuiCtrlCreateButton ("OK",180,130,70,20)

GuiSetState()

While 1
   $msg = GUIGetMsg()
 

  Select
     Case $msg = $GUI_EVENT_CLOSE Or $msg = $cancelbutton
        ExitLoop
        
     Case $msg = $okbutton
        $date_111= _FormatDate($dtFrom, $dStyle , 0)
        msgbox(0,$date_111,_DateIsValid($date_111))
        ExitLoop
    EndSelect
WEnd

msgbox(0,"yyyymmdd",_FormatDate($dtFrom, $dStyle , 1))

GUIDelete()
Exit

Func _FormatDate($ctrl,$style,$delimit)

   local $result[4]
   $sequence = StringSplit(StringUpper($style), "\/")

   $data = GUICtrlRead($ctrl)
   $data = StringSplit($data, "\/")

   For $i = 1 to 3

     if $sequence[$i] = 'D' then
          $result[3] = $data[$i]
     elseif $sequence[$i] = 'M' then
          $result[2] = $data[$i]
     else;StringLeft($sequence[$i],1) = 'Y' then
         If Number($data[$i]) < 1000 then
            if Number($data[$i]) > 50 Then
              $data[$i] = "19" & $data[$i]
            Else
              $data[$i] = "20" & $data[$i]
            EndIf
         EndIf
         $result[1] = $data[$i]
      endif
   Next
  
   if $delimit = 0 then
      return $result[1] & "/" & StringRight("0" & $result[2],2) & "/" & StringRight("0" & $result[3],2)
   else
      return $result[1] & StringRight("0" & $result[2],2) & StringRight("0" & $result[3],2)
   endif

EndFunc

I'd like to see GuiCtrlCreateDate to always supply access to a valid date

Share this post


Link to post
Share on other sites

yyyymmdd seqence for most date functions - very frustrating - try this

#include <Date.au3>

; _dateIsValid requires YYYY/MM/DD
$sDate = @YEAR & "/" & @MON & "/" & @MDAY

If _DateIsValid( $sDate ) Then
  MsgBox( 4096, "Valid Date", "The specified date is valid." )
Else
  MsgBox( 4096, "Valid Date", "The specified date is invalid." )
EndIf

Share this post


Link to post
Share on other sites

yyyymmdd seqence for most date functions - very frustrating - try this

#include <Date.au3>

; _dateIsValid requires YYYY/MM/DD
$sDate = @YEAR & "/" & @MON & "/" & @MDAY

If _DateIsValid( $sDate ) Then
  MsgBox( 4096, "Valid Date", "The specified date is valid." )
Else
  MsgBox( 4096, "Valid Date", "The specified date is invalid." )
EndIf
When I created the Date UDF's we decided to use the "International Standard ISO 8601" date notation.

Thats why all Date UDFs use that particular format.

Should be quit easy to create an UDF to reformat into this standard format.


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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
Sign in to follow this  
Followers 0