Jump to content

Trouble with dates...


Jewtus
 Share

Recommended Posts

I always struggle with date functions... This is what I'm trying to do.

Based on a given date, check to see if a 2nd date is closer to the first date (but not after) than the first of the year of the first date.

Example 1

Date1=5/5/2014

Date2=5/5/2013

Now in this case, 5/1/2014 is closer to the first of the year than date 2, so I would want to display 1/1/2014.

Example 2

Date1=5/5/2014

Date2=2/5/2014

In this case, the date 2 is closer to the first number, so I would want to display 2/5/2014.

This is what I have in my code so far, but it doesn't work the way I want it to.

$Date1='5/5/2015'
    $Date2='2/5/2014'
    $D1Split=StringSplit($Date1,"/")
    $D2Split=StringSplit($Date2,"/")
    If $Date1 > "1/1/"&$D2Split[3] Then
        MsgBox(0,"Date1 bigger","1/1/"&$D1Split[3])
    Else
        MsgBox(0,"Date2 bigger",$Date2)
    EndIf

I feel like I'm missing an and in the if statement but I have been racking my brain trying to figure it out.

I've even tried using datediff and trying to work with those numbers so it was easier for me to wrap my head around (I have trouble with dates and date calculations) and I still couldn't figure it out.

Link to comment
Share on other sites

  • Moderators

Jewtus,

I would do it like this: :)

#include <Date.au3>

$sDate_1 = "2014/01/05"
$sDate_2 = "2013/05/05"

$sDate_Ret = _CheckDates($sDate_1, $sDate_2)
If @error Then
    MsgBox($MB_SYSTEMMODAL, "Error", "Date 2 is after Date 1")
Else
    MsgBox($MB_SYSTEMMODAL, "Return", $sDate_Ret)
EndIf


$sDate_1 = "2014/05/05"
$sDate_2 = "2014/05/02"

$sDate_Ret = _CheckDates($sDate_1, $sDate_2)
If @error Then
    MsgBox($MB_SYSTEMMODAL, "Error", "Date 2 is after Date 1")
Else
    MsgBox($MB_SYSTEMMODAL, "Return", $sDate_Ret)
EndIf



Func _CheckDates($sDate_1, $sDate_2)
    ; Derive first day of year
    $sDate_First = StringLeft($sDate_1, 5) & "01/01"
    ; Check date2 before date1
    If _DateDiff("D", $sDate_1, $sDate_2) > 0 Then
        Return SetError(1, 0, "")
    Else
        ; Now get differences between dates and datefirst
        $iDiff_1 = _DateDiff("D", $sDate_First, $sDate_1)
        $iDiff_2 = _DateDiff("D", $sDate_2, $sDate_First)
        ; And return closest to datefirst
        If $iDiff_1 < $iDiff_2 Then
            Return $sDate_1
        Else
            Return $sDate_2
        EndIf
    EndIf
EndFunc
Obviously you need to add some date formatting code if you want to use a different format for the input - you might want to look at my Date_Time_Convert UDF which makes it very easy to do. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

  • Moderators

SorryButImaNewbie,

Why not look in the Help file at what is returned from the _DateDiff function before asking questions like that? ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

M23

I did that of course, i growth a bit in checking before asking thanks to you guys, it said "Returns the difference between 2 dates, expressed in the type requested" but i dont see where one can give that type, I mean okey i can tell that give me it back in seconds or days or what everelse. But does it give it back in string format ?

edit: groth to growth

edit2: I seems to fail to recall what is the name of the number format in a variable... (like string, boolean and aaaaa Edit3: its integer... for god's sake...) it made my search for it quite hard, but found in autohotkey that if a variable only contains numbers it automaticly take it as a number, but in autoit we can do it like: $var = "2" and $var = 2, so im not sure here, keep worming the help file :)

Edited by SorryButImaNewbie
Link to comment
Share on other sites

M23

Thanks, that pointed me in the right direction. I had actually already worked with the datediff, but the if conditional you laid out helped me figure out what I was doing wrong (I didn't have the datediff in the correct order which was giving me negative numbers).

This is the function that did what I needed:

Func _CheckDates($sDate_1, $sDate_2)
    If StringRight($sDate_1,4) > StringRight($sDate_2,4) Then
        Return '01/01/'&StringRight($sDate_1,4)
    ElseIf _DateDiff("D", StringRight($sDate_1,4)&'/'&StringLeft($sDate_1,6), StringRight($sDate_2,4)&'/'&StringLeft($sDate_2,6)) > 0 Then
        Return SetError(1, 0, "")
    Else
        $sDate_First = "01/01/"&StringRight($sDate_1, 4)
        ; Now get differences between dates and datefirst
        $iDiff_1 = _DateDiff("D", $sDate_First, $sDate_1)
        $iDiff_2 = _DateDiff("D", $sDate_2, $sDate_First)
        ; And return closest to datefirst
        If $iDiff_1 < $iDiff_2 Then
            Return "01/01"&StringLeft($sDate_1, 5)
        Else
            Return $sDate_2
        EndIf
    EndIf
EndFunc
Link to comment
Share on other sites

  • Moderators

SorryButImaNewbie,

The answer is there - try looking at this page. ;)

Basically $var = "2" stores the assigned value as a string, while $var = 2 stores it as a number (integer in this case). _DateDiff returns a number - it is the type of period (month, day, second, etc) required that you set. But the number is not an integer - as you can see here: ;)

#include <Date.au3>

$sDate_1 = "2014/01/05"
$sDate_2 = "2013/05/05"

$iDiff = _DateDiff("D", $sDate_2, $sDate_1)

MsgBox($MB_SYSTEMMODAL, "Return", $iDiff & @CRLF & VarGetType($iDiff))
Clearer now? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...