Sign in to follow this  
Followers 0
dar100111

Can't get _DateIsValid to work in my loop

5 posts in this topic

#1 ·  Posted (edited)

First off,  I didn't have <Date.au3> included at first and now I do.  I thought it would have received an error but maybe not.

Is there a reason why my If Statement wouldn't work for _DateIsValid in my loop with array that I have?

#include <String.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <INet.au3>
#include <excel.au3>
#include <array.au3>
#include <Date.au3>
#include <OutlookEX.au3>

$oExcel = _ExcelBookOpen( @ScriptDir & "\Bulk Email Tool.xlsm")
_ExcelSheetActivate($oExcel, "Contacts")
Global $contactarray = _ExcelReadSheetToArray($oExcel, 2, 1, 0, 2)
_ExcelSheetActivate($oExcel,"Info")
Global $aArray = _ExcelReadSheetToArray($oExcel);This will contail all the info to send to the branches
$rows = $aArray[0][0]
$cols = $aArray[0][1]
;Checking if dates need to be formatted through the whole array
For $i = 1 To $rows Step 1
    For $c = 0 to $cols Step 1
        $check = $aArray[$i][$c]
        MsgBox("", "", $check)
        If _DateIsValid($check) Then
            $date_converted = StringRegExpReplace($check, '(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)', '$1/$2/$3 $4:$5:$6')
            $aArray[$i][$c] = $date_converted
        EndIf
    Next
Next

_ArrayDisplay($aArray)

The array can contain date values of Excel that get converted to "20140417000000" format.

The code below works as follows individually.  Just not in my loop.  I'm probably doing something incorrect but can't seem to figure it out.

Can anyone see the error?

$string = "20140417000000"
Local $sDate = StringRegExpReplace($string, '(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)', '$1/$2/$3 $4:$5:$6')
; msgbox(0,"", $sDate)

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

My array contains examples below.  (just copied these from the array display function.

MIA|231817893|230244171|20140416000000|65.2|16

SFO|231818809|230148118|20140417000000|197.88|15

ORD|231819883|230025639|20140418000000|560|14

 

Edited by dar100111

Share this post


Link to post
Share on other sites



You're not converting the date before you check it, you're trying to do that afterwards.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

It seems to be looping through everything like I want.

 

I have also just tried the below and it doesn't catch it either.

For $i = 1 To $rows Step 1
    For $c = 0 to $cols Step 1
        MsgBox("", "", $aArray[$i][$c])
        If _DateIsValid($aArray[$i][$c]) Then
            $aArray[$i][$c] = StringRegExpReplace($aArray[$i][$c], '(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)', '$1/$2/$3 $4:$5:$6')
        EndIf
    Next
Next

Share this post


Link to post
Share on other sites

Was I unclear?

In your second example you convert the raw date string into a format that _DateIsValid can recognize. So it passes validation.

In your loop code, you're grabbing the data from excel into an array, and then trying to compare the raw data, with the function _DateIsValid, which of course is going to fail because instead of being formatted text, you're trying to validate "MIA|231817893|230244171|20140416000000|65.2|16" as a date, and it clearly isn't.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Gotcha Brew Thanks.  I was thinking that conversion would cause an error. Thank you sir.  Just needed a good eye.

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