Jump to content

_DateToNumber()


d2addict4
 Share

Recommended Posts

I was making this because I needed to get the info from a date ctrl, and convert it to month-day-year or have it in a array. So heres the code:

; #FUNCTION# ;===============================================================================
; Name...........: _DateToNumber
; Description ...: Converts the information recieved from GUICtrlCreateDate() into MONTH-DAY-YEAR (In numbers) Or
;                  It will return it in an array
; Syntax.........: _DateToNumber($_InfoReturnedFromDateCtrl, $ActualDateIs=1)
; Parameters ....: $_InfoReturnedFromDateCtrl - The information that is returned from a GUICtrlCreateDate() Control.
;                  $ActualDateIs=1 - If $ActualDateIs = 1 Then It will return it as MONTH-DAY-YEAR, if it is 0 It
;                  will return it as an array $nArray[0] = 3 (MONTH-DAY-YEAR) $nArray[1] = MONTH, $nArray[2] = DAY, $nArray[3] = YEAR
; Return values .: Success - Returns either the MONTH-DAY-YEAR, or MONTH-DAY-YEAR in an array
;                  Failure - Returns 0 and sets @Error.
;                  |1 = $_InfoReturnedFromDateCtrl (Param 1) Is Blank
;                  |2 = $ActualDateIs is not 1 or 0
; Author ........: D2Addict
; Modified.......: None
; Remarks .......: None
; Related .......: _NumberToDate() (Not completed yet)
; Link ..........; http://www.autoitscript.com/forum/index.php?showtopic=70969
; Example .......; No
;
; ;==========================================================================================
Func _DateToNumber($_InfoReturnedFromDateCtrl, $ActualDateIs = 1)
    If $_InfoReturnedFromDateCtrl="" Then Return SetError(1, 0, 0)
    If Not $ActualDateIs = 1 Or $ActualDateIs = 0 Then Return SetError(2, 0, 0)
    $ActualDate = ""
    $_String = StringSplit($_InfoReturnedFromDateCtrl, " ")
    $Day = StringTrimRight($_String[3], 1)
    $Month = ""
    If $_String[2] = "January" Then $Month = "01"
    If $_String[2] = "February" Then $Month = "02"
    If $_String[2] = "March" Then $Month = "03"
    If $_String[2] = "April" Then $Month = "04"
    If $_String[2] = "May" Then $Month = "05"
    If $_String[2] = "June" Then $Month = "06"
    If $_String[2] = "July" Then $Month = "07"
    If $_String[2] = "August" Then $Month = "08"
    If $_String[2] = "September" Then $Month = "09"
    If $_String[2] = "October" Then $Month = "10"
    If $_String[2] = "November" Then $Month = "11"
    If $_String[2] = "December" Then $Month = "12"
    $ActualDate = $Month & "-" & $Day & "-" & $_String[4]
    If $ActualDateIs = 0 Then
        $String_ = StringSplit($ActualDate, "-")
        Return $String_
    EndIf
    Return $ActualDate
EndFunc   ;==>_DateToNumber
Edited by d2addict4
Link to comment
Share on other sites

no comments? for real? :)

Not to be too harsh, but there's a lot wrong with this as a function anybody can use.

It works for you I'm sure, but what about somebody who doesn't have their "Long date" format (Control Panel, Regional Settings) set up the same way you do?

What if their date separator character is set to "-" instead of " " ?

What if they create the date control with the $DTS_SHORTDATEFORMAT style?

A lot more work needed before this is fit for public consumption. :(

Edit: spelling

Edited by ResNullius
Link to comment
Share on other sites

I just got around to looking at this and some of the problems are solved below. It does not solve all of the regionalization issues though. I'll leave that part for someone else. This includes test code.

;
#include <array.au3>
GUICreate("TEST")
$dc = GUICtrlCreateDate("",10,10)
GUISetState()
While 1
  Switch GUIGetMsg()
    Case -3
      Exit
    Case $dc
      MsgBox(0, "Date",_DateToNumber(GUICtrlRead($dc)))
  EndSwitch
Wend

Func _DateToNumber($sDateCtrl, $rStr = 1)
  If $sDateCtrl="" Then Return SetError(1, 0, 0)
  If $rStr > 1 Or $rStr < 0 Then $rStr = 1
  Local $m, $aMth, $I, $Month = "", $ActualDate = ""
  For $I = 1 To StringLen($sDateCtrl)
    $sChr = StringMid($sDateCtrl, $I,1)
    If NOT StringIsAlNum($sChr) Then $sDateCtrl = StringReplace($sDateCtrl, $sChr, Chr(32))
  Next
  $_String = StringSplit(StringStripWS($sDateCtrl, 7), Chr(32))
  $mArray = StringSplit("Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec", "|")
  For $I = 1 To $mArray[0]
    $aMth = _ArraySearch($_String, $mArray[$I], 1, 0, 0, 1)
    If $aMth <> -1 Then ExitLoop
  Next
  $Day = $_String[3]
  $m = _ArraySearch($mArray, StringLeft($_String[$aMth], 3))
  $Month = ""
  $Month = StringFormat("%02u",$m)
  $ActualDate = $Month & "-" & $Day & "-" & $_String[4]
  If $rStr = 0 Then
    $String_ = StringSplit($ActualDate, "-")
    Return $String_
  EndIf
  Return $ActualDate
EndFunc  ;<===>_DateToNumber()
;

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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...