Skysnake

Valid DATE formats and manipulation

3 posts in this topic

Hi

This is a request to streamline various Date and Time functions.  (PS My ability to log a ticket appears to be limited by an uncooperative server).

Let met start by saying these functions all work.  This is not an error report.

The documentation appears accurate.

 

My problem started here:

page autoit.chm::/html/libfunctions/_DateAdd.htm

contains "$sDate     Initial date in the format YYYY/MM/DD[ HH:MM:SS]"

 

This is in stark contrast to eg 

_DateIsValid

"This function takes a date input in one of the following formats:
"yyyy/mm/dd[ hh:mm[:ss]]" or "yyyy/mm/dd[Thh:mm[:ss]]"
"yyyy-mm-dd[ hh:mm[:ss]]" or "yyyy-mm-dd[Thh:mm[:ss]]"
"yyyy.mm.dd[ hh:mm[:ss]]" or "yyyy.mm.dd[Thh:mm[:ss]]""

or 

my own personal, do-anything option

_GUICtrlDTP_SetFormat :)

I understand that DTP and DateTime is not exactly the same.  That is not the issue.

My problem is with being able to create and TEST a valid date in at least 3 different formats, but not being able to use additional functions on such a valid date.  Assume my format is  yyyy-mm-dd based on the  "yyyy-mm-dd[ hh:mm[:ss]]" or "yyyy-mm-dd[Thh:mm[:ss]]" _DateIsValid() function.  I now have a valid function, but cannot readily use it with any of the internal DateTime functions.

Would it be possible to consider modifying the existing functions to work with the standard formats.  Put simply, if a format is accepted by _DateIsValid(), all other DateTime formats should work with same date (format) directly.

Once I have an accepted date, I want to use all internal functions on it. 

Thank you for your kind attention.

Skysnake


Skysnake

Why is the snake in the sky?

Share this post


Link to post
Share on other sites



I am running AutoIt version 3.3.14.2.  And, if you look at the User Defined (date) Functions in the Date.au3 include file,  you should notice _DateIsValid() is used to check the dates that enter any of the functions with date as a parameter, and written by Jos.    It appears the Date functions are already valid _DateIsValid() format compatible.

In the AutoIt help file it says to use "YYYY/MM/DD[ HH:MM:SS]" format for the $sStartDate, $sEndDate,  and $sDate parameters of the _DateDiff(), _DateTimeFormat(), and  the _DateAdd() functions.   The help file should include all of the valid _DateIsValid() formats for these mentioned parameters, as shown in the _DateTimeSplit function in the AutoIt help, in my opinion.

The Date UDF's return dates in the "yyyy/MM/dd hh:mm:ss" format.    So I have included _DateTimeFormatEx function.  This UDF will convert _DateIsValid formats, including the "yyyy/MM/dd hh:mm:ss" format, to any format.  So the original date format can be easily retrieved after using the Date functions, if so desired.

#include <Date.au3>

; Note date formats are Not in the "yyyy/MM/dd hh:mm:ss" format, yet still work.
ConsoleWrite(_DateDiff('h', "1970-01-01T14:00:00", "1970.01.02 00:00") & " hrs" & @CRLF) ; Returns 10 hrs
ConsoleWrite(_DateTimeFormat("1970.11.01T14:00:00", 0) & @CRLF) ; Returns 01/11/1970 2:00:00 PM

ConsoleWrite(_DateTimeFormatEx(_DateAdd('s', 36001, "1970-01-01T13:30:00"), "yyyy-MM-ddTHH:mm:ss") & @CRLF) ; Returns 1970-01-01T23:30:01 (same format as input date format)
ConsoleWrite(_DateTimeFormatEx("1970.11.01T02:30:00", "yyyy-MMMM-dd H:mm:ss") & @CRLF) ; Returns 1970-November-01 2:30:00
ConsoleWrite(_DateTimeFormatEx("1970.11.01T02:30:00", "MM.dd.yyyy") & @CRLF) ; Returns 11.01.1970


; Format date time using a format string.
; $sDate: input date in any of the valid _DateIsValid function formats.
; Valid DateIsValid() formats:-
;   "yyyy/MM/dd[ hh:mm[:ss]]" or "yyyy/MM/dd[Thh:mm[:ss]]"
;   "yyyy-MM-dd[ hh:mm[:ss]]" or "yyyy-MM-dd[Thh:mm[:ss]]"
;   "yyyy.MM.dd[ hh:mm[:ss]]" or "yyyy.MM.dd[Thh:mm[:ss]]"
;   (Remove optional square brackets for use in the $sFormat parameter.)
;$sFormat: A string representing the format for the output appearance.
;   See _GUICtrlDTP_SetFormat function in AutoIt Help file for format string characters.
;
Func _DateTimeFormatEx($sDate, $sFormat = "yyyy/MM/dd hh:mm:ss")
    ; Verify If InputDate is valid
    If Not _DateIsValid($sDate) Then
        Return SetError(1, 0, "")
    EndIf
    $hGui = GUICreate("")
    $idDate = GUICtrlCreateDate($sDate, 10, 10)
    GUICtrlSendMsg($idDate, 0x1032, 0, $sFormat) ; or "dddd, MMMM d, yyyy hh:mm:ss tt"); or "hh:mm tt"
    $FormatedDate = GUICtrlRead($idDate)
    GUIDelete($hGui)
    Return $FormatedDate
EndFunc   ;==>_DateTimeFormatEx

 

1 person likes this

Share this post


Link to post
Share on other sites

Dear @Malkey, thank you! This saves a huge amount of work. Testing now :)

Skysnake


Skysnake

Why is the snake in the sky?

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

    • Skysnake
      By Skysnake
      SQLite has this:
          https://www.sqlite.org/lang_datefunc.html
          Compute the last day of the current month.
          SQL: SELECT date('now','start of month','+1 month','-1 day');
      This is my take on the calculating the end of the month in AutoIt. I simply followed the logic of the SQLite.
      ; Compute end of current month Local $sNewDate = _DateAdd('m', 1, _NowCalcDate()) $sNewDate = _DateAdd('d', -1, $sNewDate) MsgBox($MB_SYSTEMMODAL, "5", "End of this month: " & $sNewDate) Is this the _only_ way? Best way?