Jump to content

_NowDate Format Help


Recommended Posts

hi guys so automating an entering of a date. when i use _NowDate() it gives the result - 28/10/2013 however i am only wanting it to 281013 - is there a way to format nowdate to produce this? if not how can i accomplish this? it must be ddmmyy exact format with no slashes. i also need to to display yesterdays date in the same format

Link to comment
Share on other sites

A quick way of doing this would to use StringReplace and StringSplit. Example:

#include <date.au3>
$date = StringReplace(_NowDate(), "/", "")
MsgBox(0, "Today", $date)

$yesterday = StringSplit(_NowDate(), "/", 2)
$subtractDay = Number($yesterday[1]) - 1
$yesterdayDate = $yesterday[0] & $subtractDay & $yesterday[2]
MsgBox(0, "Yesterday", $yesterdayDate)
Link to comment
Share on other sites

Here's a more advanced version. Hopefully, it will show you how functions can work:

#include <date.au3>

; _GetDate is a function that is written below. We can pass information to a function and use it to return a result.
$date = _GetDate("") ; $date is a variable calling the function _GetDate.
MsgBox(0, "Today", $date)
 
$date = _GetDate("Yesterday") ; Pass the word "Yesterday" to the function below
MsgBox(0, "Yesterday", $date)

Func _GetDate($functionPassVariable)
    $date = _NowDate() ; Today's date
    $dateSplit = StringSplit($date, "/", 2) ; Split string
    If $functionPassVariable = "Yesterday" Then ; If we sent the word "Yesterday", then subtract 1 day from the date
        $dateSplit[1] = Number($dateSplit[1]) - 1
    EndIf
    ; The next line uses StringTrimLeft to shorten 2013 to just 13
    Return($dateSplit[0] & $dateSplit[1] & StringTrimLeft($dateSplit[2], 2)) ; Returns the date back to the varible that called this function
EndFunc
Link to comment
Share on other sites

13lack13lade,

If you want to do date arithmetic then you should use _nowcalc as it returns a date in the format that _dateadd() and _datediff() expect.

The following does what you want...see the comments in the code...

#include <date.au3>

local $mydate, $offset

$mydate = cnvtdate(-2)
ConsoleWrite('Two days ago - ' & $mydate & @LF)

$mydate = cnvtdate()
ConsoleWrite('Today - ' & $mydate & @LF)

$mydate = cnvtdate(15)
ConsoleWrite('Fifteen days from now - ' & $mydate & @LF)

func cnvtdate($offset = 0)

    $date = _Dateadd('D', $offset, _nowcalcdate())              ; get date with offset
    $adate = stringsplit($date,'/',3)                           ; split date into parts for rformatting
    return $adate[2] & $adate[1] & stringright($adate[0],2)     ; return date / stringright returns only the two rightmost bytes of the year

endfunc

@abberation - what if the date is 10/01/2013?

 

kylomas

edit : simplified code pre mikells comments below

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Hum there is a trick because of the time formats so you should use _NowCalcDate()

#include <date.au3>

$today = _NowCalcDate()
MsgBox(0, "today", _Convert($today) )

$yesterday = _DateAdd('d', -1, $today)
MsgBox(0, "yesterday", _Convert($yesterday) )

Func _Convert($date)
   Return StringRegExpReplace($date, '\d\d(\d\d)/(\d\d)/(\d\d)', '$3$2$1')
EndFunc

Straight from the example in the helpfile for StringRegExpReplace :)

Edited by mikell
Link to comment
Share on other sites

Hi mikell,

Yes, I stayed away form regexp because

i am not great at coding but learning string replacements etc

 

and the thread was already down that path.

The point was to show how to present the date in a format suitable for further processing and an example of ome simple string processing.

Thanks for your input and I agree, _nowcalcdate() is easier for a beginner to work with!

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Guys this is a little too deep. This is much simpler.

;

Global $sDate = "28/10/2013"

$sDate = StringReplace($sDate, "/", "") ; Replace "/" with an empty string
MsgBox(0, "Removed Delimiter", $sDate)

$sDate = StringLeft($sDate, 4) & StringRight($sDate, 2) ; Concatenation
MsgBox(0, "Removed century", $sDate)
Edited by czardas
Link to comment
Share on other sites

mikell, your example with regexp is probably the best approach. I just wanted to chime in with the discussion and answer a more fundamental question. :)

... is there anyway to change 2013 to just 13? like a trim function perhaps? 

Edited by czardas
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...